1

I'm having a little trouble getting the following bit of code to work correcty in my script:

if (email == "") {
    $("#field2 .error1").show();
    $("input#email").focus();
    return false;  
    }
else if(email != /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/) {
    $("#field2 .error2").show();
    $("input#email").focus();
    return false;  
    }

Basically I have a form on my page and it's validating a couple of fields using a external js file. I want it to check if the email field is blank or if it doesn't match the regular expression. If it's blank it returns .error and if it's not matching the regular expression I want it to show .error2.

What's happening is it's showing the errors but even if I put in a correct email address it won't validate and just continues showing .error2. I'm pretty stumped what I'm missing here so any help would be appreciated.

Even if I try this,

if (email == "" || email != /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/) {
    $("#field2 .error1").show();
    $("input#email").focus();
    return false;  
    }

I still can't get it to send the form???


OK So I've tried this and it seems to be working properly. Does it look correct? lso I'll take into consideration the regx string and use a better one, it was more just to get it working as intended.

var email = $("input#email").val();
var regx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/;   
if (email == "") {
    $("#field2 .error").show();
    $("input#email").focus();
    return false;  
}
else if (!regx.test(email)){
    $("#field2 .error2").show();
    $("input#email").focus();
    return false;
}
JJJ
  • 32,902
  • 20
  • 89
  • 102
Shaun
  • 11
  • 1
  • Lots of other threads here on SO about using a regular expression to validate an email address. You can find all of them with a simple search. Here's a popular one: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – jfriend00 Sep 16 '11 at 03:33

4 Answers4

2

Try this

    var regexp=/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;

if(regexp.test(email)){
   // matched
  }else{
   // not matched
   }
Bhanu Krishnan
  • 3,726
  • 1
  • 20
  • 40
0

Here try this Head:

<script type="text/javascript">
function validateEmail(theForm) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.email-id.value))
    {
        return(true);
    }
    alert("Invalid e-mail address! Please enter again carefully!.");
    return(false);
}
</script>

Body:

<form onSubmit="return validateEmail(this);" action="">
E-mail Address:
    <input type="text" name="emailid" />
    <input type="submit" value="Submit" />
    <input type="reset" value="Reset" />
</form>
Elliot Wood
  • 964
  • 1
  • 9
  • 29
0

you need do do like this,

 var regx=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/  // create your regular expresseion

 if(regx.test(email)){// use test() method to do the match
   // do if match
  }else{
   // not matched
   }
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • I'm confused as I left the match part blank, I thought I would test it and it worked. Don't I need to stick something in the matched section? I have: var email = $("input#email").val(); if (email == "") { $("#field2 .error1").show(); $("input#email").focus(); return false; } var regx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/ if(regx.test(email)){ // do if match }else{ $("#field2 .error2").show(); $("input#email").focus(); return false; } – Shaun Sep 16 '11 at 03:50
  • I can't understand what you are saying here .. If you don't need to use matched section just don't use it. – Jayantha Lal Sirisena Sep 16 '11 at 04:19
0

What you are comparing is a regular expression.

 var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/;

 if (!filter.test($("input#email").val())) 
 {

    // invalid email

 }
 //valid email
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 2
    The regex used in the question and all the answers so far fails many valid email addresses. The name part should be able to contain the characters !#$%&'*+-/=?^_`{|}~ and if part of the address appears in quote marks, virtually any character. [Source](http://en.wikipedia.org/wiki/Email_address#Syntax) These rules would filter out valid (but unusual) email addresses like Paul+1@example.com or Timothy"@"Jones#3@example.org – thomasrutter Sep 16 '11 at 03:45