Click here to Skip to main content
13,630,992 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 29 Nov 2011
Licenced CPOL

JavaScript to validate the user input

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Sample code to validate the data using JavaScript

Perform Validations using JavaScript:

Validating the web forms is the most important and common task in developing applications. Validating the page without allowing the user to post the page to server enhances the user experience. In this section, we will see how to validate the web form by using client side JavaScript.

Access the ASP.NET Server Controls using JavaScript

You can access the ASP.NET server controls by using the following script:
var employeeid = document.getElementById('<%= TextBox1.ClientID %>');

Similarly, you can access all other controls in the form.

Make textbox accept only digits

The following function is used to make a text box to accept only digits.
function isNumberKey(evt) {
        if (charCode = evt.which) {
            charCode = evt.which;
        else {
            charCode = evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        else {
            return true;

Validate the emailid

Emaild can be validated by checking the position of ‘@’ and ‘.’ characters in the emailid as follows:
var atpos = emailid.value.indexOf("@");
var dotpos = emailid.value.lastIndexOf(".");

   if ((inputDOJ.value == "") || (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= inputDOJ.length)) 
         lbl3.innerHTML = "Not a valid e-mail address";
         flag = 1;

Check if the date is greater than today:

You check the entered date with the date object of the JavaScript as shown below:

var DOJ = new Date(inputDOJ.value);
var todaydate = new Date();
var month = todaydate.getMonth() + 1;
var today = new Date(month + '/' + todaydate.getDate() + '/' + todaydate.getFullYear());
if ((inputDOJ.value == "") || (DOJ < today)) 
          lbl2.innerHTML = "Date should be greater than today"
          flag = 1;

Check the values to be distinct

You can check if the entered values in the two fields are distinct by using the following code:
if (reportingmanger.value == employeeid.value)
      lbl6.innerHTML = "Reporting MangerID should not be same as EmployeeID";
       flag = 1;

Check if the values in the fields are blank

Javascript can be used to make the fields as mandatory by using the following code snippet:
if (reportingmanger.value == "") 
         lbl6.innerHTML = "ReportingManagerID cannot be blank";
         flag = 1;

Check the length of employeeid

Length of the value entered in the field can be checked by using 'length' property.
if ((employeeid.value == "" ) ||  (employeeid.value.length != 4))
            lbl1.innerHTML = "Please Enter Valid 4 digit EmployeeID";
            flag = 1;


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


About the Author

S V Saichandra
Software Developer Collabera
Singapore Singapore
S V Sai Chandra is a Software Engineer from Hyderabad Deccan. He started Embedded Programing in his college days and now he is a Web Developer by Profession. He Loves coding and his passion has always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books, painting and hang out with friends is his most favorite past time hobby.
He blogs at
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.

You may also be interested in...


Comments and Discussions

GeneralisNumberKey: you should allow NumPad, backspace, tab, backsp... Pin
stian.net30-Nov-11 2:57
memberstian.net30-Nov-11 2:57 
GeneralRe: thanks for the info Pin
S V Saichandra30-Nov-11 4:43
memberS V Saichandra30-Nov-11 4:43 
GeneralRe: i will update the code sooner Pin
S V Saichandra30-Nov-11 4:43
memberS V Saichandra30-Nov-11 4:43 

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 | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180712.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by S V Saichandra
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid