datatype_void datatype_void - 2 years ago 238
Javascript Question

Converting C# Regular Expression to Javascript Compatible Variant

I am currently building some custom form validators and using regular expressions to test the validity of email address against. I got the

from MSDN | How to: Verify that Strings Are in Valid Email Format and attempted to convert it using RegexBuddy, but I get the following error:

RegexBuddy Error

I am not exactly a
wizard but some help would be greatly appreciated. I think that alternation could be used for the conditional, but I am greatly unsure of the

Edit: If you read the comments on that SO post you'll see that none of those are perfect, and this approach is documented by Microsoft, so it's a little more tested than many of those.

Answer Source

JavaScript does not support look-behinds or conditionals.

This is the regex that I use for email validation in my custom forms. It works great in JavaScript.


And a version that supports non ASCII characters, though be careful as some programs may have trouble processing non ASCII characters.


As always, remember that client-side validation is no replacement for server-side validation and is mainly to improve user experience.

Here is a very detailed explanation about how to choose an email regex including a reference to the practical limitations of TLD length based on the capability of SMTP (Max 63 characters) and explanation of the official standard RFC 5321

You can find out about lookbehinds and conditionals on this site:

Experiment with regular expressions here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download