Click here to Skip to main content
Click here to Skip to main content

Email Validation in JavaScript

, 12 Nov 2012 CPOL
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');
    email.focus;
    return false;
 }
}</script>

Now you have successfully validate your HTML controls through JavaScript.

License

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

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 1022527225-Jan-14 1:51 
Questionvalidation PinmemberPascualito2-Nov-13 11:52 
GeneralMy vote of 2 PinmemberJerry OLoughlin9-Sep-13 8:29 
SuggestionIssue Pinmembervikastin6-Feb-13 20:43 
GeneralMy vote of 2 PinmemberSelvin19-Nov-12 14:00 
GeneralNice one PinmemberKill the BUG14-Nov-12 23:24 

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

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

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