Duminda Chamara

Tuesday, November 3, 2009

Email Address Validation in Javascript

How to validate an email address using JavaScript?

Following example shows you how to validate an email address in javascript as a client side validation.
We need to validate email address very often. When you register in a web site it prompt your email address. An email address has a predefined format with it. If you entered a email address that not match the pattern, the web application shows you a message that please enter a valid email address. In most cases they use javascript validation as primary validation in forms. You can use Regular Expression to validate the email address. The pattern check the your input is matched email address or not. If it is not a valid email address It indicates that your input is incorrect and re-enter it. E-mail address have two parts (someone@example.com). The part before the @ sign is the local-part of the address, often the username of the recipient (someone), and the part after the @ sign is the domain which is a hostname to which the e-mail message will be sent. every valid email address must contain username, @ sign and domain name.


Demo- How to validate an email address using JavaScript.
Email address validation
1.Enter valid email address. view code
Try it!
Bookmark and Share

1. Example code for validate an email address using JavaScript.

JavaScript Code
<script type="text/javascript" language="javascript"> <!-- function validateEmailAddress(){ var emailAddress = document.getElementById("emailAddress").value; var regExEmail = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; if(emailAddress==''){ alert("Please enter a valid email address"); document.getElementById("emailAddress").focus(); return false; }else{ if(!regExEmail.test(emailAddress)){ alert("Please enter a valid email address"); document.getElementById("emailAddress").focus(); return false; } } alert("OK\nYour email address is in correct format"); return true; } --> </script>
Html Code
<html> <head> <title>How to validate email address in JavaScript </title> </head> <body> <table border="0"> <tr> <td valign="top"> 1.Enter valid email address. </td> <td align="left"> <input name="emailAddress" type="text" id="emailAddress" size="40" /> </td> <td align="left">&nbsp;</td> </tr> <tr> <td align="right">&nbsp;</td> <td align="left" > <input name="btnEmailAddressValidator" type="submit" id="btnEmailAddressValidator" value="Validate" onclick="javascript:validateEmailAddress();" /> </td> <td align="left">&nbsp;</td> </tr> </table> </body> </html>
» How to get screen resolution in JavaScript
» How to limit characters in textarea using JavaScript
» How to validate decimal number in JavaScript
» How to validate an email address in JavaScript
» How to validate date using JavaScript
» JavaScript String functions
» How to validate multiple select list box in JavaScript
» How to generate random numbers in JavaScript
» How to validate multiple check box in JavaScript
» How to validate user login in JavaScript
» How to validate drop down list in JavaScript
» How to validate radio button group in JavaScript
» How to create JavaScript alerts
» How to create popup windows in JavaScript
» How to count words in a text area using JavaScript
©-Copyright By Duminda Chamara JavaScript Validation

0 comments: