Search code examples
asp.net-mvcasp.net-mvc-4email-validation

Regular expression for email doesnt work in mvc cshtml


i have a Regular expression like

 var filter = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);

in mvc,but it shows an error after the @ in cshtml page which is obvious. how can i rectify it so that it can work correctly.Thanks for any assistance.

P.S. Do provide if anyone have any other Regular expression.Thanks


Solution

  • Escape @ as @@, i.e. this should work:

    var filter = new RegExp(/^[+a-zA-Z0-9._-]+@@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
    

    I like to use Phil Haack's C# Razor Syntax Quick Reference as a, well... quick reference to Razor's syntax.