Tuesday, 15 December 2009

ASP.NET RegularExpressionValidator cannot do a case insensitive match

ASP.NET has its own way of doing things that can be completely different from everything else. Most of the time I can live with this but now and again it is really annoying and I came across one of those times this week.

The task I wanted to achieve was to validate an email field to see check if the user had entered a valid email. Its an obvious job for a regular expression and I had some javascript code from another project that did the job.

Here is the expression.....

/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i

Regular expressions are reasonably standard across different platforms so I did not think I would have a problem to use this in ASP.NET.

The RegularExpressionValidator is designed for the job, but it does not accept the '/i' at the end, which sets the regular expression to be case insensitive. Instead there is a Microsoft syntax '?i:' .

(?i:(^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)))

Using the above expression  RegularExpressionValidator will check for a valid case insensitive email address on the server side.

However  RegularExpressionValidator also checks for a valid email on the client side by generating javascript code to do the job. Unfortunately the above expression will generate an error as it is not compatible with javascript!

Why they could not make ASP.Net regular expressions compatible with the javascript standard I dont know.

In looking for a solution I came across this one, where the guy uses Javascript to do a search and replace on regular expression text when the page loads. It should work, but what a terrible cludge.

http://reflectedthought.com/thecoder/archive/2007/11/25/regularexpressionvalidator_ignore_case.aspx

In my case it was possible to rewrite the regular expression so it could be used on both the client and server sides. Next time I may not be so lucky.

So the validator to check for valid email addresses is as follows.

<asp:RegularExpressionValidator ID="emailRegularExpressionValidator1" runat="server"
    ErrorMessage="A valid email address must be supplied"
    ControlToValidate="person_email"
    ValidationExpression='^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-zA-Z]{2,6}(?:\.[a-zA-Z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)'></asp:RegularExpressionValidator>

3 comments: