Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted

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.
 
Share this answer
 
Comments
Ranjith Reddy CSE 21-Jul-12 5:37am    
Please can you show me , how to do that, Please help Christian Graus. I need your help.
Christian Graus 21-Jul-12 5:41am    
I suggest buying a book on ASP.NET and reading it, then perhaps one on javascript. You might also investigate using google to look for online articles. https://www.google.com.au/search?sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8&q=javascript+validate+date is a good place to start
XML
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.
 
Share this answer
 
v3
You can use javascript code for validating the Date in TextBox.
Write the following JavaScript code:-
JavaScript
function CalculateAge(birthday)
     {
       var re = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
         if (birthday.value != '')
            {
               if (re.test(birthday.value))
                {
                  return true;
              else
                 {
                   alert('Date must be mm/dd/yyyy format');
                   return false;
                  }
             }


Now in TextBox onblur event call this JavaScript function:-
ASP.NET
<asp:textbox id="txtAge" runat="server" onblur="CalculateAge(this)" />

For more Please Visit this link:-
Regular expression to validate date in mm/dd/yyyy format in JavaScript[^]
Good luck.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900