Tarasov
Tarasov

Reputation: 3683

Why is my Regular Expression not work in ASP.NET

I want use a RegularExpressionValidator for a date in this form yyyy-mm-dd (example: 2012-11-29) and here is my expresion:

/^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$/

I test it on http://www.quanetic.com/Regex and it works but if I do this in my asp.net application it doesn't work

 <tr>
   <td>Gültig ab:</td>
   <td><asp:TextBox ID="txtVon" runat="server" ></asp:TextBox></td>  
   <td><asp:ImageButton ID="imgVon" runat="server" ImageUrl="images/Calender.ico" Width="15" Height="15" />
     <asp:CalendarExtender runat="server" ID="E_Von" TargetControlID="txtVon" Format="yyyy-MM-dd" PopupButtonID="imgVon"/></td>        
   <td>
     <asp:RequiredFieldValidator ID="ValVon" 
       runat="server" ForeColor="red" 
       ErrorMessage="*" ControlToValidate="txtVon"></asp:RequiredFieldValidator>
     <asp:RegularExpressionValidator ID="regVon"   
       runat="server" ControlToValidate="txtVon" 
       ErrorMessage="*Format" ForeColor="red" 
       ValidationExpression="/^(19[789]\d|20[0123]\d)\-(0\d|1[012]|\d)\-(31|30|[012]\d|\d)$/"></asp:RegularExpressionValidator>
   </td>
 </tr>

Where is the error?

Upvotes: 1

Views: 2712

Answers (2)

UnitStack
UnitStack

Reputation: 1185

I use the following, which works ok.

\A(?:^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$)\Z

Upvotes: 1

og Grand
og Grand

Reputation: 114

Just remove char "/" in the begining and in the and of the string.

And you will have

ValidationExpression="^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$"

Upvotes: 5

Related Questions