如何验证GridView中的文本框以正确输入日期 [英] How to validate textbox inside gridview to enter date correctly

查看:92
本文介绍了如何验证GridView中的文本框以正确输入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何验证gridview中的文本框以正确输入日期
================================================== ============

我的网页上有一个Gridview,可以选择编辑和删除该Gridview中的数据.

假设如果单击DELETE链接,它将删除特定的行...
如果我单击编辑"链接,它将显示用于编辑文本的文本框....这很不错.

我有一个文本框可以编辑日期.用户必须输入正确的日期,例如21 \ 07 \ 2012.
如果用户这样输入日期849489383443,则必须显示一条消息,请输入验证日期.


谢谢.

How to validate textbox inside gridview to enter date correctly
===============================================================

I have a Gridview on my webpage, I have options to edit and delete the data from that gridview.

Suppose if click the DELETE link it will delete the particular row...
and if i click the edit link it will show the textboxes to edit the text....THIS ONE IS FINE.

I have a textbox to edit the date. user must enter the correct date like this 21\07\2012.
if the user enter date 849489383443 like this, it must show a message please enter validate date.


Please help me , THANKS.

推荐答案

最简单的方法是在onblur中调用一个javascript方法,因为它很容易编写并放入您的gridview模板,因此每个文本框都会调用它.使用体面的日期控件更有意义,但是如果您被文本框所困扰,那就是这样做的方法. Javascript支持正则表达式以供您验证.
The most obvious way to do this, is to have a javascript method that is called in the onblur, because that is easy to write and to put in to the template for your gridview, so it''s called for each textbox. Using a decent date control makes more sense, but if you''re stuck with a textbox, that''s the way to do it. Javascript supports regular expressions, for your validation.


Alternate way is use ajax calender extendar & mask edit extender controls with RangeValidatior validation control

in following way


 <asp:TextBox runat="server" ID="MyTextBoxDateOfBirth" CssClass="textboxInlineCalendar"

                                        Text='<%# Bind("FieldName", "{0:dd/MM/yyyy}") %>'></asp:TextBox>
                                    <br />
                                    <asp:Label ID="Label1" runat="server" Text="(dd/MM/yyyy)"></asp:Label>
                                    <br />
                                    <asp:RangeValidator ID="RangeValidator1" runat="server"

                                        ControlToValidate="MyTextBoxDateOfBirth" Display="None"

                                        ErrorMessage="Invalid Date." Type="Date"

                                        MaximumValue="1/1/2050" MinimumValue="1/1/1753"

                                        ValidationGroup="ValidationGroupName">
                                    </asp:RangeValidator>
                                    <ajax:CalendarExtender ID="MyTextBoxBuyDateCalendar" TargetControlID="MyTextBoxDateOfBirth"

                                        Format="dd/MM/yyyy" runat="server" />
                                    <ajax:MaskedEditExtender ID="MaskedEditExtender1" runat="server" TargetControlID="MyTextBoxDateOfBirth"

                                        MaskType="Date" InputDirection="LeftToRight" MessageValidatorTip="true" Mask="99/99/9999">
                                    </ajax:MaskedEditExtender>


Note ValidationGroup of edit button & RangeValidator1 Must be Same.


您可以使用JavaScript代码来验证TextBox中的日期.
编写以下JavaScript代码:-
You can use javascript code for validating the Date in TextBox.
Write the following JavaScript code:-
function CalculateAge(birthday)
     {
       var re = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+


这篇关于如何验证GridView中的文本框以正确输入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆