In a C# 2010 web form application, I would like to know how to setup user server side validators to check the format of a date in mm/dd/yyy format. Basically this would be like a regular expression, custom validator, compare validator. I basically need to validate the mm/dd/yyyy format includes for month, date, year. I also am required to make certain the user enters the date with the '/'. enter.
Can you tell me how to accomplish this goal?
Loading
Deepak VermaPosted Jul 26, 2012, 10:57 PM
Link 1 and Link 2
scampercatPosted Jul 26, 2012, 11:44 AM
scampercatPosted Jul 26, 2012, 11:43 AM
1. What does \d{2} mean? two occurences of a number?
2. What does \/ mean? This means to use / as a real value and not use it as a regular expression meaning?
Deepak VermaPosted Jul 26, 2012, 6:23 AM
Jignesh TrivediPosted Jul 26, 2012, 6:13 AM
Yes...
so why we put two validator RegularExpressionValidator and CustomValidator on single control we can acheive this using single CustomValidator .
I think if i Enter any unvalid date like "45/66/2012", it is not work @ client side.
it work when page validate method is called.
correct me if i am wrong.
Deepak VermaPosted Jul 26, 2012, 5:16 AM
I've tested it! Just working great...
The
Jignesh TrivediPosted Jul 26, 2012, 4:01 AM
i have tried your solution and enter data like "45/66/2012" so it allow me to enter.
Regex.IsMatch("45/66/2012", @"^\d{2}\/\d{2}\/\d{4}$" ) is return true.
we required date format "mm/dd/yyyy" so first 2 digit must be lessthan or equal to 12.
thx.
Deepak VermaPosted Jul 26, 2012, 3:36 AM
Actually both of above solutions will validate "02/29/2011" or "02/30/2012" as correct, while the given dates are invalid.
The solution is here:
ASPX CODE:
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="DateValidation" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" ErrorMessage="
Date not formatted well!" Display="Dynamic" ControlToValidate="TextBox1" ValidationExpression="\d{2}\/\d{2}\/\d{4}" ForeColor="Red" ValidationGroup="DateValidation">asp:RegularExpressionValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="
Date not formatted well!" OnServerValidate="CustomValidator_OnServerValidate" ForeColor="Red" Display="Dynamic" ValidationGroup="DateValidation" />
<asp:Button ID="Button1" runat="server" Text="click" ValidationGroup="DateValidation" />
C# CODE:
protected void CustomValidator_OnServerValidate(object sender, ServerValidateEventArgs args)
{
try
{
DateTime.ParseExact(args.Value, "MM/dd/yyyy", null);
args.IsValid = true;
}
catch
{
args.IsValid = false;
}
}
Jignesh TrivediPosted Jul 26, 2012, 12:49 AM
you can use regular expression like
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$
it validate the date.
valid : 12/12/2000, 05/25/2012
invalid : 23/12/2005
hope this will help you.
Deepak VermaPosted Jul 25, 2012, 11:38 PM
you can use this RegularExpressionValidator :
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="
Date not formatted well!" Display="Static" ControlToValidate="TextBox1" ValidationExpression="\d{2}\/\d{2}\/\d{4}" ForeColor="Red" >asp:RegularExpressionValidator>