1

I am putting validation on email field but it shows error of invalid even where the email is typed in correct format.

Screenshot

Code

<script type="text/javascript">
function formvalidate(){
    var email=document.signup_form.email.value;

    var check_email= RegExp("^[A-Z0-9._-]+@[A-Z0-9.-]+\.[A-Z0-9.-]+$");

    if(email=="")
    {
        alert("cannot be empty!");
        document.signup_form.email.focus();
        return false;
    }

    else if(!check_email.test(email))
    {
        alert("enter valid email address!");
        document.signup_form.email.focus();
        return false;
    }
    else
    {
        return true;
    }
}

</script>

Thanks

Natha Odedara
  • 206
  • 3
  • 9

10 Answers10

1

1- Use this regular expressions instead

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

2- Change this if (!check_email.test(email)) to

if (check_email.test(email))
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
Ali Niazi
  • 19
  • 3
1

try this function

function validateEmail(elementValue) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
}

Please refer this fiddle : http://jsfiddle.net/gabrieleromanato/Ra85j/

NikuNj Rathod
  • 1,663
  • 1
  • 17
  • 26
0

Try this fuction

     function isEmail(inputString) {
     var regExpEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
         try {
              return regExpEmail.test(inputString.value);
             }
        catch (e) {
              return false;
               }

}

Nikhil Ghuse
  • 1,258
  • 14
  • 31
0

Change var check_email= RegExp("^[A-Z0-9._-]+@[A-Z0-9.-]+\.[A-Z0-9.-]+$"); to a function like this:

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\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 re.test(email);
}
KO.
  • 173
  • 3
  • 18
0

You regex is not correct, there are many things that you've not considered, like your regex accepts only capital letters, to include both capital and small letters you should use :

[a-zA-Z0-9]

not this :

[A-Z0-9]

You can use this regex for validating email :

/^(([^<>()\[\]\\.,;:\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,}))$/

read this for details

Community
  • 1
  • 1
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
0

Source and some tests

You can use this regex for email:

RegExp('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b', 'i')
Justinas
  • 41,402
  • 5
  • 66
  • 96
0

try this example : email validation using JQuery

function validateEmail(email) {
  var re = /^(([^<>()[\]\\.,;:\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 re.test(email);
}

function validate() {
  $("#emailvalidate").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#emailvalidate").text(email + " is valid");
    $("#emailvalidate").css("color", "green");
  } else {
    $("#emailvalidate").text(email + " is not valid");
    $("#emailvalidate").css("color", "red");
  }
  return false;
}
$("form").bind("submit", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='btn'>Validate</button>
</form>
<h2 id='emailvalidate'></h2>
Mahendra Kulkarni
  • 1,437
  • 2
  • 26
  • 35
0

Try this code -

function formvalidate()
{
    var email = document.signup_form.email.value;

    var check_email = RegExp("^([a-z0-9\\+_\\-]+)(\\.[a-z0-9\\+_\\-]+)*@([a-z0-9\\-]+\\.)+[a-z]{2,6}$", 'ig');

    if(email == "")
    {
        alert("cannot be empty!");
        document.signup_form.email.focus();
        return false;
    }

    else if(!check_email.test(email))
    {
        alert("enter valid email address!");
        document.signup_form.email.focus();
        return false;
    }
    else
    {
        return true;
    }
}
<form name="signup_form">
  <input type="text" name="email" value="" />
  <input type="button" name="validate" value="Validate" onclick="formvalidate()"/>
</form>
Vikash Kumar
  • 1,091
  • 9
  • 16
0

I am using below regex to validate email pattern. Regex is not 100% solution to validate an email, better use email verification. Regex can help to validate format or pattern only.

Jsfiddle: DEMO

Jsfiddle: Regex check and sample email DEMO

function validateEmail(elementValue) {
  document.getElementById('error').innerHTML = elementValue + ', email is incorrect';
  var emailPattern = /^[a-z0-9](?:[a-z0-9]+\.)*(?!.*(?:__|\\.\\.))[a-z0-9_]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9](?!$)){0,61}[a-zA-Z0-9]?)|(?:(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])))$/;
  if (emailPattern.test(elementValue)) {
    document.getElementById('error').innerHTML = elementValue + ', email is correct';
  }
  return false;
}
#error {
  color: red;
  font-size: 2rem;
}

input[type='email'] {
  padding: 5px;
  width: 300px;
  border-color: blue;
  font-size: 16px;
}
<form name="signup_form">
  <input type="email" value="" onblur="validateEmail(this.value)">
  <br />
  <lable id="error"></lable>
</form>
Nono
  • 6,986
  • 4
  • 39
  • 39
-1

Change your regular expression to below

^[a-zA-Z0-9._-]+@[A-Za-z0-9.-]+\.[a-zA-Z0-9.-]+$

Hope this helps :)

Mr.7
  • 2,292
  • 1
  • 20
  • 33