gmail validation in javascript code example
Example 1: javascript regex email
function validateEmail (emailAdress)
{
let regexEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (emailAdress.match(regexEmail)) {
return true;
} else {
return false;
}
}
let emailAdress = "[email protected]";
console.log(validateEmail(emailAdress));
Example 2: email regex javscript
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());
}
Example 3: Email validation using javascript
function checkEmail() {
var email = document.getElementById('txtEmail');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}
- Call the function on Email textbox
Example 4: how to validate an email address in javascript
function validateEmail(email)
{
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
console.log(validateEmail('[email protected]'));
Example 5: Email Validation in JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Email validation</title>
<script type="text/javascript">
function email_funnction() {
var email = document.emailform.email.value;
if(email.indexOf('@')<=0){
document.getElementById('errormsg').innerHTML="Invalid your Email Address";
return false;
}
if ((email.charAt(email.length-4)!='.') && (email.charAt(email.length-3)!='.')) {
document.getElementById('errormsg').innerHTML="Invalid your Email Address";
return false;
}
}
</script>
</head>
<body>
<h1>Email Validation in javascript</h1>
<span id="errormsg"></span>
<form name="emailform" onsubmit="return email_funnction()">
<label>Email</label>
<input type="text" name="email" value="">
<input type="submit" name="" value="Submit">
</form>
</body>
</html>