I was taking a look on the jquery.validate plugin source code, to check which expressions are being used to check the validity of a field, so I can implement the same in server side. I got into the Regex for email, for example:
return this.optional(element) ||/^((([a-z]|\d|[!#\$%&’*+-/=\?\^_
{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+))|((\x22)((((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))(((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?$/i.test(value);
I guess, that the part in bold is the expression, but it doesn’t work in .NET (it says the email wrong all the time) I guess it is because the javascript encoded characters. Is there an easy way I can use to translate the expression?
Thanks.
Although it is commercial (i.e. non-free, but cheap) I could not recommend “RegexBuddy” http://www.regexbuddy.com/ highly enough.
Using a standard “standard” RegEx syntax (which you can interactively build and test) it will then generate the source code in correct syntax for use in several environments and many “scenarios” including .net, javascript, Perl, PHP, Python etc.
With my lacklustre knowledge of Regex, this program is a lifesaver.
* disclaimer: No affiliation whatsoever – just a very happy multi-year customer
** Extra note — I just notice that Jeff Attwood has a testimonial on their homepage!
JavaScript:
.net C#