Validating email address in put using Javascript can be tricky. Contrary to popular belief, email addresses can contain almost all type of characters and a valid email address can take complex forms.
For example, the email addresses below are perfectly valid:
- firstname+lastname@domain.com
- much.”more\ unusual”@example.com
- あいうえお@a.long.domain.example.university
One of the straight forward way to validate an email address is using regular expressions Here is a moderately reasonable email validation function using regular expression:
Useful Javascript Email validation function using regex
function validateEmail(email)
{
var re = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
return re.test(email);
}
Does the email address actually exist?
If the purpose of your validation is more complex and want to test whether the email address actually exist, then the validation becomes quite complex. You will have to depend on SMTP API methods to test it. The validation itself can take some time so not advisable to do it from the browser. You may have to make use of a web server, API and a queue to complete the validation process in some time.
The time and effort required to make a test on whether the email exists, makes it impossible to give live feedback to the user.
If you think that the purpose of validation is to alert user of an unintentional mistake while entering the email, or is just tprevent a casual lurker from submitting the form filled with junk, the regular expression validation above should work just fine.
If you want to verify the ownership and validity of the email, then sending a confirmation link to the email address will be a better method.
Most simple Javascript email validation
If you love super simplicity that gets the job done, just checking for the presence of one @
symbol will do the job, likeso:
function validateEmail(email)
{
var re = /\S+@\S+/;
return re.test(email);
}
Let the browser do the validation
You can make use of the HTML5 feature and let the browser do the validation. No need of any JavaScript
<label for="my_form_email">Your Email:</label>
<input id="my_form_email" type='email' name='email' required />
See also:
- A Definitive Guide To Sensible Form Validations
- I Knew How To Validate An Email Address Until I Read The RFC
- How to Find or Validate an Email Address
See Also
- How to Use the Dreamweaver Form Validation Feature
- JavaScript Form Validation : quick and easy!
- JavaScript Form Validation Script: More features
- Simfatic Forms Validation: save your time coding
- JavaScript Form Validation - Confirm Password
- JavaScript Form Validation - Date
- JavaScript Form Validation - Phone Numbers
- Javascript password validation alphanumeric string
- Javascript password validation using regular expression
- Javascript validation simple example code