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

3

10 Answers 10

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/

Sign up to request clarification or add additional context in comments.

Comments

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)) 

1 Comment

I used this expression but still shows invalid error on valid email ([email protected])
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; } 

}

Comments

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); } 

Comments

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

Comments

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') 

Comments

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>

Comments

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>

Comments

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>

Comments

-1

Change your regular expression to below

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

Hope this helps :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.