Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one textbox and also one MaskedEditExtender to attache with that text box for entering value in this formate like dd-MMM-yyyy HH:mm....
now i want to put one RegularExpressionValidator to validate that text box.....


i have put it like this...

ASP.NET
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("ABOVEROTARYTABLE", "{0:d-MMM-yyyy HH:mm}") %>'>
<cc1:MaskedEditExtender ID="MaskedEditExtender4"  runat="server" ClearMaskOnLostFocus="False"
    CultureName="en-GB" Mask="99-LLL-9999 99:99" TargetControlID="TextBox9" UserDateFormat="DayMonthYear">

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox9"
ErrorMessage="Invalid DateTime" 
 ValidationGroup="GridViewBHAHistory"
ValidationExpression=""


now i want regular expression to validate my input.....

Valid like
12-Oct-2011 22:34
23-dec-1988 02:09


or not valid like
43-TTT-8888 88:98
12-oct-2011 25:39



help me....
Posted
Comments
Member 8388026 16-Nov-11 3:39am    
try this \d{2}/\d{2}/\d{4} go to the regular expression properties and in that write a expression in:validate expression \d{2}/\d{2}/\d{4}
Member 8388026 16-Nov-11 3:39am    
this will validate it to dd/mm/yyyy
Mehdi Gholam 16-Nov-11 3:40am    
Why don't you use DateTime.TryParse() instead of regular expressions?

I wouldn't use a Regex for this: it is too difficult to get it right, and handle the number of days in the appropriate month.
Instead, look at using DateTime.TryParseExact:
C#
DateTime dt;
if (DateTime.TryParseExact("12-Oct-2011 22:34", "dd-MMM-yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
    {
    Console.WriteLine(dt);
    }
 
Share this answer
 
Comments
Mehdi Gholam 16-Nov-11 3:52am    
Let the framework do the work! 5'ed
RaisKazi 30-Jul-14 13:28pm    
Agree, 5ed.
try this \d{2}/\d{2}/\d{4} go to the regular expression properties and in that write a expression in:validate expression \d{2}/\d{2}/\d{4}

this will validate it to dd/mm/yyyy
 
Share this answer
 
for my solution i made this regular expression to validate datetime in this formate

dd-MMM-yyyy HH:mm


this is the regular expression use

C#
"^(0?[1-9]|[12][0-9]|3[01])-(jan|Jan|JAN|feb|Feb|FEB|mar|Mar|MAR|apr|Apr|APR|may|May|MAY|jun|Jun|JUN|jul|Jul|JUL|aug|Aug|AUG|sep|Sep|SEP|oct|Oct|OCT|nov|Nov|NOV|dec|Dec|DEC)-(19|20)\d\d\s([0-1][0-9]|[2][0-3]):([0-5][0-9])$"
 
Share this answer
 
v2
Try this it may help you :)

(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)
 
Share this answer
 
validationexpression="^(?:((31-(Jan|Mar|May|Jul|Aug|Oct|Dec))|((([0-2]\d)|30)-(Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))|(([01]\d|2[0-8])-Feb))|(29-Feb(?=-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))-((1[6-9]|[2-9]\d)\d{2})$"

replace symbol after ? with : and ((
 
Share this answer
 
v2
You can use this to primarily validate your input. But it's not all. You cannot check for valid time/date value. User can input 99-jan-9999 99:99
\d{2}-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}\s\d{2}:\d{2}
 
Share this answer
 

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