Validate Email address

Is a dynamic computer programming language.[5] It is most commonly used as part of web browsers, whose implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed.[5] It is also used in server-side network programming with runtime environments such as Node.js, game development and the creation of desktop and mobile applications.
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Validate Email address

Post by dzcadii » Sun Mar 01, 2015 10:57 pm

Simple way to validate an email address!
You still have to validate on the server side!
This will only alert the user of invalid input.

Email address patterns accepted:(.com,.net,.edu.,.mil,.gov, etc)

Code: Select all

// JavaScript Document
function check_email(){
	var Email_Pattern = new RegExp("^[a-zA-Z0-9_\.]{2,}@{1}(([a-zA-z]{2,}\.{1}[a-zA-Z]{2,6})|([a-zA-Z]{2,}\.{1}[a-zA-Z]{2,}\.[a-zA-Z]{2,6})|([a-zA-Z]{2,}\.{1}[a-zA-Z]{2,}\.[a-zA-Z]{2,}\.[a-zA-Z]{2,6}))$");
	
	var email_address = document.getElementById('Email_Address');
		if(!Email_Pattern.test(email_address.value)){
			email_address.style.backgroundColor = "red";
			alert("Invalid email address!\nIt needs to follow a pattern like:\nsomeone@someplace.com!\nThe field has been cleared!");
			email_address.value = "";
		}
}
This is how you would use it (All together):

Code: Select all

<html>
<head>
<title>I am checking the email address</title>
<script language="javascript">
function check_email(){
   var Email_Pattern = new RegExp("^[a-zA-Z0-9_\.]{2,}@{1}(([a-zA-z]{2,}\.{1}[a-zA-Z]{2,6})|([a-zA-Z]{2,}\.{1}[a-zA-Z]{2,}\.[a-zA-Z]{2,6})|([a-zA-Z]{2,}\.{1}[a-zA-Z]{2,}\.[a-zA-Z]{2,}\.[a-zA-Z]{2,6}))$");
   
   var email_address = document.getElementById('Email_Address');
      if(!Email_Pattern.test(email_address.value)){
         email_address.style.backgroundColor = "red";
         alert("Invalid email address!\nIt needs to follow a pattern like:\nsomeone@someplace.com!\nThe field has been cleared!");
         email_address.value = "";
      }
}
</script>
</head>
<body>
<form method="post" action="something.html">
<table>
<tr><td>Email Address:</td></tr>
<tr><td><input type="text" name="Email_Address" onChange=check_email();" id="Email_Address"></td></tr>
</table>
</form>
</body>
</html>

Post Reply