Welcome to Webune

today we are going to share a helpful function in javascript which you can use to validate a user's email address once they hit the submit button.

lets say you have an html web form and before the information is submited you want check if the email address is valid or correct.

its very easy, you can just use a function.

this is how it works, check out this demo:
DEMO:

Please Enter Your Email Address:


ok, so you are wondering how did we do it, here is the code we used:

<script type="text/javascript">
// VALIEDATE EMAIL:
function ValidateMail(Email) {
 	 // NOT A VALID FORMAT
	var RegularExpression1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
 	 // GOOD EMAIL - VALID FORMAT
	var RegularExpression2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;		
	// CHECK IF THE EMAIL MATCHING OUR REGULAR EXPRESSIONS
	if (!RegularExpression1.test(Email) && RegularExpression2.test(Email)) {
		return true;
	}
	return false;
}
function CheckEmail(){
	var Email =  document.getElementById("Email").value;
	if(ValidateMail(Email)){
		window.alert("Congratulations. This is a valid Email!");
	}else{
		window.alert("Error. This is a NOT valid Email!");
	}
}
</script>
Please Enter Your Email Address: <input type="text" name="Email" id="Email"><input type="button" value="Check Email" OnClick="CheckEmail();">


thanks for visiting webune. please check out our other helpful tutorials on javascript