1

I have ASP.NET project and I need to check is email correct or not. I always using that script, but it fails with asp.net, because @ is bad symbol.

function emailvalidate(e)
{
    var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
    return b.test(e);
}

How can I fix this? And its important to check email by JavaScript, not ASP.NET or C#.

P.S. Im really new with ASP.NET and JavaScript RegEx.

Timus
  • 59
  • 4
  • just try to escape @. – Avinash Raj Jun 24 '14 at 15:50
  • 3
    "*but it fails with asp.net, because @ is bad symbol.*" What? What exactly wrong with your JavaScript? Are you getting any errors or unexpected results? – p.s.w.g Jun 24 '14 at 15:51
  • http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – karthikr Jun 24 '14 at 15:52
  • I see no problem with your code - assuming it is part of some static .js file (where it should belong) I see no reason why `@` would be a problem. Possibly you are missing some more information (i.e. maybe you rendering this script on page using Razor, or something similar), but at this point it is not possible to say what answer would be correct. – Alexei Levenkov Jun 24 '14 at 16:04

3 Answers3

5

Are you using the Razor view engine? If so you will need to escape those @ symbols if you are adding this script to the cshtml file itself

function emailvalidate(e)
{
    var b=/^[^@@\s]+@@[^@@\.\s]+(\.[^@@\.\s]+)+$/;
    return b.test(e);
}

or you can wrap the regular expression in @Html.Raw as deherch mentioned:

var b = @Html.Raw(@"/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/");

you can also add this function to a script file with all your other common JS functions. This way you do not need to escape the razor syntax at all.

Win mentions that you want to use server-side validation. I would say best to use both. Client-side validation can stop it from ever hitting the server in the first place. And server-side validation because you can never trust what a client passes in.

PaulBinder
  • 2,022
  • 1
  • 16
  • 26
  • Might be easier to wrap the regular expression in `@Html.Raw` like so: `var b = @Html.Raw(@"/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/");` – deherch Jun 24 '14 at 15:59
  • Very true! Thanks deherch – PaulBinder Jun 24 '14 at 16:01
  • 2
    Side note: Visual studio might complain about it not being valid javascript when you are editing the .cshtml file even though the resulting html/js is perfectly valid. If you want to get rid of that warning u can just add `+""`, so then it would become: `var b = @Html.Raw(@"/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/")+"";` I wish Visual studio was smarter :) – deherch Jun 24 '14 at 16:03
2

In ASP.Net, you want to use server side MailAddress to validate email, because regular expression itself is not enough to validate an email address.

try 
{
    address = new MailAddress(EMAIL_ADDRESS).Address;
} 
catch(FormatException ex) 
{
   // Log 
}

If you really want to use regular expression, here is the link which will make you cry.

Win
  • 61,100
  • 13
  • 102
  • 181
  • The question specifically states: **And its important to check email by JavaScript, not ASP.NET or C#.** I agree with your statement in general, but it is not really the answer to the question. – deherch Jun 24 '14 at 16:10
0

Try Below code for Server side validation of Email

public bool IsValid(string emailaddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

Or You can also see below link

Reference URL