Click here to Skip to main content
Licence CPOL
First Posted 15 Nov 2009
Views 7,734
Downloads 96
Bookmarked 16 times

Generic Code of Validating Fields with Jquery

By Pranay Rana | 15 Nov 2009
Generic code of validating fields with Jquery
2 votes, 28.6%
1

2

3
1 vote, 14.3%
4
4 votes, 57.1%
5
4.00/5 - 7 votes
μ 4.00, σa 3.31 [?]

Introduction

This article demonstrates how to write generic code for validating form input fields and to present the required field in a different form. In this, I have created code that is reusable and it's applicable to not only ASP.NET website but can also be applied to any PHP or other language website. Below is the final output of the code:

Background

Before starting this code, I always wondered how to represent my required field so it becomes more user friendly for it to be understood by the end user. To find out a solution for this, I went through the basic JQuery book named JQuery in action. JQuery is nowadays known to most of the web developers which is next door to Java library providing an easy way to code JavaScript and manipulate the form element.

Using the Code

As mentioned earlier, this article represents the required fields in a different manner to the end user of the web application and validates form input fields. Following is the code description which gives you a more detailed idea of how it works. Let's start with the creation of style sheet which is used to represent fields on form. I have created two style sheets as you see in the below code template:

  • .mandatory- It is the CSS class for the required fields to represent fields in pink format
  • .normal – It gets applied to input field after text is entered by the end user in text box.
<style>
.mandatory
{
   background-color<span class="code-none">: Pink<span class="code-none">;
   font-size<span class="code-none">: 15px<span class="code-none">;
<span class="code-none">}

.normal
<span class="code-none">{
   background-color<span class="code-none">: Transparent<span class="code-none">;
   font-size<span class="code-none">: 15px<span class="code-none">;
<span class="code-none">}
</style></span></span></span></

After style sheet, the following code describes the JavaScript used: Following is the list of functions:

After document gets loaded completely, jquery library ($(document).ready) function gets called first which in turn calls the callOnload function: callOnload() is the function that finds all input elements which have attribute isvalidate with the value true. After that, it applies mandatory class to all found input fields and assigns onkeyUp function to each. Onkeyup function gets called when the user enters a value in the input field which assigns class normal if value is entered by the end user and it assigns mandatory class if value is removed from the input field. ("#btnSubmit").click – This line of code finds click function with submit. So when button gets clicked, it finds all input elements with attribute isvalidate having value true and checks value of it. If the value is empty, it accesses value of errpMsg attribute and displays message to the end user.

<script type="text/javascript">

    <script src="JavaScript/jquery.js" type="text/javascript"></script>
   function callOnload()
   {
      $("input[isvalidate=true]").each(
                              function(n)
                              {
                                 $('#' +this.id).addClass('mandatory');
                                 this.onkeyup=function()
                                 {
                                    if(this.value==='')
                                    {
                                       $('#' +this.id).removeClass('normal');
                                       $('#' +this.id).addClass('mandatory');
                                    }
                                    else
                                    {
                                       $('#' +this.id).removeClass('mandatory');
                                       $('#' +this.id).addClass('normal');
                                    }
                                 }
                              }
                        );

         $("#btnSubmit").click(
                              function()
                              {
                                  $("input[isvalidate=true]").each(
                                  function(n)
                                  {
                                     if(this.value==='')
                                     {
                                        alert($('#' +this.id).attr('errprMsg'));
                                     }
                                  }
                                 );
                                return false;
                              }
                           );
   }
   $(document).ready(callOnload);
</script>

Following is the HTML code of the page in that you look for the following attributes which are associated with the input fields of the form: isvalidate= true means it is mandatory isvalidate= false means it is non-mandatory errprMsg= message gets displayed when the field is empty.

<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" Width="150px" ID="lbl1" Text="User Name"></asp:Label>
<asp:TextBox runat="server" ID="txt1" isvalidate="true" 
	errprMsg="Enter value in User Name"></asp:TextBox>
</div>
<div>
<asp:Label runat="server" Width="150px" ID="Label1" Text="Password"></asp:Label>
<asp:TextBox runat="server" TextMode="Password" ID="TextBox1" 
	isvalidate="true" errprMsg="Enter value in Password"></asp:TextBox>
</div>
<div>
<asp:Label runat="server" Width="150px" ID="Label2" Text="Email id"></asp:Label>
<asp:TextBox runat="server" ID="TextBox2" isvalidate="false" 
	errprMsg="Enter value in Email id"></asp:TextBox>
</div>
<div>
<asp:Button runat="server" ID="btnSubmit" Text="Validate" />
</div>
</form>
</body>

Points of Interest

With the above code, the developer will get an idea that JQuery can create its new library which allows the developer to code their JavaScript easily without writing too much messy JavaScript code.

Reference

  • JQuery in Action

History

  • 12th November, 2009: Initial version

License

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

About the Author

Pranay Rana

Software Developer (Senior)
GMind Solusion
India India

Member

Follow on Twitter Follow on Twitter
Hey, I am Pranay Rana, working as a Senior Software engineer. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 
You can visit me on my blog - http://pranayamr.blogspot.com/
StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:



Best ASP.NET article of July 2010



Best ASP.NET article of March 2010


2nd In ASP.NET article of December 2010

3rd In C# article of December 2010

4th In Overall article of December 2010

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberNaveen.25590:03 19 Jan '10  
GeneralPoor article, invalid HTML and frontend only PinmemberTom Kleijkers0:30 8 Jan '10  
GeneralMy vote of 1 PinmemberTom Kleijkers0:28 8 Jan '10  
GeneralGood Article for Jquery Beginners PinmemberMember 28212787:07 25 Nov '09  
GeneralRe: Good Article for Jquery Beginners PinmemberAnil Srivastava19:50 21 Jan '10  
My vote is five.
Good article for me.
Is there any article on Linq
Regards
Anil Srivastava
GeneralRe: Good Article for Jquery Beginners PinmemberPranay Rana23:55 23 Dec '10  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 15 Nov 2009
Article Copyright 2009 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid