Using regular expressions is probably the best way.


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(String(email).toLowerCase());
}

Here's the example of regular expresion that accepts unicode:


var re = /^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|(".+"))@(([^<>()[].,;:s@"]+.)+[^<>()[].,;:s@"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:


<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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() {
var $result = $("#result");
var email = $("#email").val();
$result.text("");

if (validateEmail(email)) {
$result.text(email + " is valid :)");
$result.css("color", "green");
} else {
$result.text(email + " is not valid :(");
$result.css("color", "red");
}
return false;
}

$("#validate").bind("click", validate);

</head>
<body>
<form>
<p>Enter an email address:</p>
<input id='email'>
<button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>
</body>

HTML5 itself has email validation. If your browser supports HTML5 then you can use the following code.


<form><input type="email" placeholder="me@example.com">
<input type="submit">
</form>

You Can use


/^[^s@]+@[^s@]+.[^s@]+$/

regular exprfession for checking the email format in different language like (Greek,english,Chinese,Japanese,Cyrillic,Latin,Unicode etc..)

It's clearly versatile and allows the all-important international characters, while still enforcing the basic anything@anything.anything format. It will block spaces which are technically allowed by RFC, but they are so rare that I'm happy to do this.


email = $('email');
filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
// Yay! valid
return true;
}
else
{return false;}

<form name="validation" onSubmit="return checkbae()">
Please input a valid email address:<br />

<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit">
</form>

<script language="JavaScript1.2">
var testresults
function checkemail(){
var str = document.validation.emailcheck.value
var filter = /^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$/i
if (filter.test(str))
testresults = true
else {
alert("Please input a valid email address!")
testresults = false
}
return (testresults)
}
</script>

<script>
function checkbae(){
if (document.layers || document.getElementById || document.all)
return checkemail()
else
return true
}
</script>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15 − 11 =