email validation through js 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: js email regex

function isValidEmail(email) {
  const 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: code for email verification in front end using javascript

function validateEmail(email) {
    const 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 4: 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>

Example 5: email validation in javascript

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation - checking email</title>
<link rel='stylesheet' href='form-style.css' type='text/css' />      
</head>
<body onload='document.form1.text1.focus()'>
<div class="mail">
<h2>Input an email and Submit</h2>
<form name="form1" action="#"> 
<ul>
<li><input type='text' name='text1'/></li>
<li> </li>
<li class="submit"><input type="submit" name="submit" value="Submit" onclick="ValidateEmail(document.form1.text1)"/></li>
<li> </li>
</ul>
</form>
</div>
<script src="email-validation.js"></script>
</body>
</html>

Tags:

Css Example