0

How to add Regular Expression validation for send mail dialog box..i have already set required field validator now i want to validate it using regular expression. It's not working properly at all..need your help..thanks in advance.

$("#submit").click(function (e) {
    var email = $("#UserEmail").val();
    var emailReg = /^([a-zA-Z0-9_\.\-])+@\[a-zA-Z0-9\-]+\.([a-zA-Z0-9]{3})+$/;
    if (!emailReg.test(email)) {
        alert("Please Enter Valid Email Id");
        e.preventDefault();
    } else {
        alert("success");
    }
});

But in the above code i am getting error at @ symbol..

ne1410s
  • 6,864
  • 6
  • 55
  • 61
  • When I attempted to format your script, the parentheses () and braces {} did not appear to match up quite right... – ne1410s Sep 17 '14 at 07:10

3 Answers3

0

Try this, it works:

var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
if(pattern.test("example@email.com")){
    alert("Valid email");
}else{
    alert("Invalid email");
}

That's it.

Viswanath Donthi
  • 1,791
  • 1
  • 11
  • 12
0

this should work:

http://fiddle.jshell.net/y1ugh71y/

var email = "myemail@yahoo.com"
    var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if ((emailReg).test(email) == false) {
        alert("Please Enter Valid Email Id");
        e.preventDefault();
    } else {
        alert("success");
    }

please refer to this post for perfect email validation method , as validation via JS is not always safe.

Validate email address in JavaScript?

Community
  • 1
  • 1
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
0

Use :

function validateEmail(mail) { 
    var validate = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return validate.test(mail);
} 
Wasif Shahjahan
  • 346
  • 2
  • 9