Saturday, 26th August 2006Javascript Email Validation with Regular Expressions
An working example of email Validation with Regular Expressions (RegEx) - just copy and paste the code!
The most accurate method of Javascript based email validation I've found to date has been using regular expressions (in fact, the most accurate method of validation I've found in VBScript for server side validation has been using Regular Expressions but thats for another article). Below is an example of RegEx based Javascript validation in use with using a html form submitting an email address to another page, the form calls the function "validateEmail" when the user tries to submit the form.
For those just looking for the Regular Expression to match an email address, its: ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$
Javascript Function
Put the code below in between the <head></head> tags of your HTML document.
<script type="text/javascript">
function validateEmail(){
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
str = document.getElementById('emailAddress').value;
if(str.match(emailRegEx)){
document.mailingListForm.submit();
}else{
alert('Please enter a valid email address.');
return false;
}
}
</script>
HTML Form
The HTML form below goes in the body of your document.
- Home
- Services
- Portfolio
- Support
- About Us
- Contact Us
Login To Your AccountSaturday, 26th August 2006Javascript Email Validation with Regular Expressions
An working example of email Validation with Regular Expressions (RegEx) - just copy and paste the code!
The most accurate method of Javascript based email validation I've found to date has been using regular expressions (in fact, the most accurate method of validation I've found in VBScript for server side validation has been using Regular Expressions but thats for another article). Below is an example of RegEx based Javascript validation in use with using a html form submitting an email address to another page, the form calls the function "validateEmail" when the user tries to submit the form.
For those just looking for the Regular Expression to match an email address, its: ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$
Javascript Function
Put the code below in between the <head></head> tags of your HTML document.
<script type="text/javascript">
function validateEmail(){
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
str = document.getElementById('emailAddress').value;
if(str.match(emailRegEx)){
document.mailingListForm.submit();
}else{
alert('Please enter a valid email address.');
return false;
}
}
</script>
HTML Form
The HTML form below goes in the body of your document.
<form action="pageToSubmitFormTo.asp" method="post" name="mailingListForm" id="mailingListForm" onsubmit="return validateEmail();">
<input name="emailAddress" type="text" size="16" id="emailAddress" />
<input name="scriptAction" type="hidden" id="scriptAction" value="joinMailingList" />
<input type="submit" name="Submit" value="Submit" />
</form>
Comment On This Article
Article Comments
Sunil, 03:07
18 July 2007
Thats Good
Copyright © 2008 Fresh Mango Technologies. All Rights Reserved.
FreshMango.com is Coded Using Standards Compliant XHTML & CSS.