Products
Services
Contact Us

Scripts: Javascript :: Forms :: Library Article #20

Developer's Section

Javascript Email Address Validator
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (1553)
Favorites:

Create a simple javascript email validator for your forms.

In this tutorial we will learn how to validate an input field that requires a valid email address.
It is definitely sometimes very important to validate data before submitting it to a database to prevent errors and / or anomalies. The following is an explanation on how to install a simple javascript that can prevent users from entering information other than a valid email address into an input field.

Email Address Validator Example:

Email Address Validator:
Enter an Email Address:


Step 1: Create Javascript method
The following code snippet shows you how to create an email address validator using a regular expression:

 Code Snippet 1

<script language="Javascript">

function validateEmail(thisEmail) {
   var objRegExp  = 
   /^([a-zA-Z0-9_\.\-\&])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   
   if(!objRegExp.test(thisEmail)) {
      alert("Please enter a valid Email Address")   
   }
   else {
        alert("email is valid")
   }
   return false
     
 }
</script>
      


Step 2: Test using a simple form
Create a form and test the function:

 Code Snippet 2

<form name="check_email" >
<table border="1" style="border-collapse: collapse">
<tr>
   <td colspan="3">Email Address Validator:</td>
</tr>
<tr>
   <td>Enter an Email Address:</td>
   <td><input type="text" name="myEmail" value="" ></td>
   <td><input type="button" value="validate"
onclick="validateEmail(document.check_email.myEmail.value)"></td>
</tr>
</table>
</form>


See other Scripts in Forms

 

Submit Your Scripts:


System Message:
  • Your Javascript script has been sent. Please allow 48 hours for processing.


If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]