0

I have a regex email validation function that i have take it from here .But the problem is when i insert it to my cs file then it shows me the errors.( it shows me lots of erors like unexpected / , ] expected,; expected )

Code

function ValidateEmail(email) {
                        var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
                        return expr.test(email);
                    }

In here i have paste the screenshots also, enter image description here

Community
  • 1
  • 1
TechGuy
  • 4,298
  • 15
  • 56
  • 87
  • 2
    I strongly recommend you use the `[EmailAddress]` attribute on your property instead (which will correctly validate the property on both the client and the server) using a correct regular expression –  Jul 26 '15 at 01:35
  • try `@Html.Raw("/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/");` – Khanh TO Jul 26 '15 at 01:45

2 Answers2

0

It's because you're trying to use @ in JS but it's recognized as a Razor expression - you see that Razor colored it yellow? you need to escape it. e.g. use @@ so Razor would understand you're not writing a Razor expression.

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
0

I think you want to use that regex in C# if yes try it like this:

[IdeOne Demo]

string email = "an.email@ahost.com";
string regex = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

Regex rgx = new Regex(regex);

Boolean result = rgx.IsMatch(email);
shA.t
  • 16,580
  • 5
  • 54
  • 111