Click here to Skip to main content
13,089,433 members (61,142 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 12 Nov 2012

Email Validation in JavaScript

Rate this:
Please Sign up or sign in to vote.
While developing a web page in fully HTML controls, you may not be able to use ASP.NET's controls, then you may need the help of some JavaScript function for validation.

What's it all about

While developing a web page in fully HTML controls, you may not be able to use the ASP.NET's controls for example if you want some validation then you can not use the regular expression control there. So there you may need this technique for validating your HTML controls.

Using the code  

Here I will show you how to validate the HTML controls using JavaScript.

Take a text input in html and a button input like this 

<input type='text' id='txtEmail'/>
<input type='submit' name='submit' onclick='Javascript:checkEmail();'/>

Now when the button is clicked then the JavaScript function SubmitFunction() will be called. Now write the bellow code in this function.

script language="javascript">

function checkEmail() {

    var email = document.getElementById('txtEmail');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
    alert('Please provide a valid email address');
    return false;

Now you have successfully validate your HTML controls through JavaScript.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

QuestionClarification of code Pin
Member 131032743-Apr-17 7:25
memberMember 131032743-Apr-17 7:25 
QuestionjQuery Email Validation Pin
Ehtesham Mehmood25-Mar-17 21:23
professionalEhtesham Mehmood25-Mar-17 21:23 
QuestionEmail validation using javascript without using an array Pin
Member 1215533120-Nov-15 2:02
memberMember 1215533120-Nov-15 2:02 
GeneralMy vote of 5 Pin
Member 1022527225-Jan-14 0:51
memberMember 1022527225-Jan-14 0:51 
Questionvalidation Pin
Pascualito2-Nov-13 10:52
memberPascualito2-Nov-13 10:52 
AnswerRe: validation Pin
alpesh_11_3_19892-Jun-15 2:02
memberalpesh_11_3_19892-Jun-15 2:02 
GeneralRe: validation Pin
Pascualito3-Jun-15 3:34
memberPascualito3-Jun-15 3:34 
GeneralMy vote of 2 Pin
Jerry OLoughlin9-Sep-13 7:29
memberJerry OLoughlin9-Sep-13 7:29 
GeneralRe: My vote of 2 Pin
alpesh_11_3_19892-Jun-15 1:58
memberalpesh_11_3_19892-Jun-15 1:58 
SuggestionIssue Pin
vikastin6-Feb-13 19:43
membervikastin6-Feb-13 19:43 
GeneralRe: Issue Pin
alpesh_11_3_19892-Jun-15 1:56
memberalpesh_11_3_19892-Jun-15 1:56 
GeneralMy vote of 2 Pin
Selvin19-Nov-12 13:00
memberSelvin19-Nov-12 13:00 
GeneralRe: My vote of 2 Pin
alpesh_11_3_19892-Jun-15 1:29
memberalpesh_11_3_19892-Jun-15 1:29 
GeneralNice one Pin
Kill the BUG14-Nov-12 22:24
memberKill the BUG14-Nov-12 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 12 Nov 2012
Article Copyright 2012 by killing the bug softly...
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid