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

Tagged as

Go to top

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;
     }

License

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

Share

About the Author

S V Saichandra
Software Developer GSS Infotech Ltd.
India India
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 is 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
http://technowallet.blogspot.com
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

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

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by S V Saichandra
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid