xVal and Regular Expression Match

Posted by gmcalab on Stack Overflow See other posts from Stack Overflow or by gmcalab
Published on 2010-03-30T20:11:30Z Indexed on 2010/03/30 20:13 UTC
Read the original article Hit count: 398

Filed under:
|
|

I am using xVal to validate my forms in asp.net MVC 1.0

Not sure why my regular expression isn't validating correctly.

It validates with the value of "12345" It validates with the value of "12345 " It validates with the value of "12345 -" It validates with the value of "12345 -1" It validates with the value of "12345 -12" ... etc

For a zip code I expect one of the two patterns:

12345 or 12345 -1234

Here are the two regex I tried:

(\d{5})((( -)(\d{4}))?)

(\d{5})|(\d{5} -\d{4})

Here is my MetaData class for xVal

[MetadataType(typeof(TIDServiceMetadata))]
public class TIDServiceStep : TIDDetail
{
   public class TIDServiceMetadata
   {
       [Required(ErrorMessage = " [Required] ")]
       [RegularExpression(@"(\d{5})|(\d{5} -\d{4})", ErrorMessage = " Invalid Zip ")]
       public string Zip { get; set; }
   }
}

Here is my aspx page:

<% Html.BeginForm("Edit", "Profile", FormMethod.Post); %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
   <td>
      <h6>Zip:</h6>
   </td>
   <td>
      <%= Html.TextBox("Profile.Zip")%>
   </td>
</tr>
<tr>
   <td>
      <input type="submit"/>
   </td> 
</tr>
</table>
<% Html.EndForm(); %>

<% Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(() =>
   { %>
   <%= Html.ClientSideValidation<TIDProfileStep>("Profile").SuppressScriptTags() %>
<% }); %>

© Stack Overflow or respective owner

Related posts about regex

Related posts about ASP.NET