July 15th in Javascript by .

Quick & Easy Form Validation Tutorial with JQuery

 

Form validation has become never been easy like before. If you havent heard of jQuery, then its time to get know now. Jquery is an open source set of javascript library, that brings user interactivity in web applications so easy even for a beginner with just few lines of code.

In this tutorial, i am going to show how easy to validate a form with jquery. For this you will need to download the following:

JQuery Libraryhttp://jquery.com
JQuery form validation pluginhttp://bassistance.de/jquery-plugins/jquery-plugin-validation/

Lets say we are going to validate a registration form like shown below..

registerdemo2.PNG

Step 1:

Place the javascript files within the head section

<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
    $("#registerForm").validate();
  });
</script>
</head>

You should set the name and id of your html form same as $(“#registerForm”).validate();

Step 2:

For each required form element just place class=”required” and specify minlength tag inside it. For example

Name field -> class=”required”
Email field -> class = “required email”

<input name="name" type="text" id="name" class="required">
<input name="user_id" type="text" id="user_id" minlength="4">
<input name="user_email" type="text" id="user_email" class="required email">

Thats it! you will be able to validate form field textboxes without any advanced code.
For optional fields, you can specify class=”optional” or just leave it.

Step 3

In this last step you will add some styles to the error messages in the stylesheet.

.error {
	font: normal 10px arial;
	padding: 3px;
	margin: 3px;
	background-color: #ffc;
	border: 1px solid #c00;
}

Thats it! see how the validation works like a charm..

registererror.PNG

Advanced Validation

The following are some of bit advanced validation code that might help you.

To check password and password retype are same

   <tr>
      <td>Password</td>
      <td><input name="pwd" type="text" id="pwd" class="required" minlength="5"></td>
    </tr>
    <tr>
      <td>Retype Password</td>
      <td><input name="pwdc" type="text" id="pwdc" class="required" equalTo="#pwd"></td>
    </tr>

To validate a website URL – optional field
It should check the URL and force the user to enter full url with http.

 <tr>
            <td>Website </td>
            <td><input name="web" type="text" id="web2" class="optional defaultInvalid url">
              <span class="example">http://www.example.com</span></td>

          </tr>

To validate Phone Number

We dont want restrict phone numbers to be US only. Suppose if a foreign person enters a phone with + and dashes, we want that as well. To do this you will need to make slight change inside document.ready() in head section.

<script>
  $(document).ready(function(){
    $.validator.addMethod("NumbersOnly", function(value, element) {
        return this.optional(element) || /^[0-9\-\+]+$/i.test(value);
    }, "Phone must contain only numbers, + and -.");

    $("#regForm").validate();
  });
  </script>

and now you can validate specifying class=”required NumbersOnly”. This will allow only numbers 0-9, hyphen and + for country code.

    <tr>
      <td>Phone</td>
      <td><input name="phone" type="text" id="phone" class="required NumbersOnly"></td>
    </tr>

Validating Username with no special characters

We dont want username to contain any special characters like dot,slashes etc. We only want to allow alphabets, numbers and underscore.

We create a function similar way like above

<script>
  $(document).ready(function(){
    $.validator.addMethod("username", function(value, element) {
        return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
    }, "Username must contain only letters, numbers, or underscore.");

    $("#regForm").validate();
  });
  </script>

and now you can call using class=”required username”

<tr>
<td>User ID</td>
<td>
<input name="user_name" type="text" id="user_name" class="required username" minlength="4">
</td>
</tr>

Enjoy jQuery!

Similar Posts:

85 Comments

  • ephrem
    August 6, 2009
    • KMS
      February 15, 2010
      • shashi
        April 13, 2012
      • shashi
        April 13, 2012
  • jaiselan
    September 10, 2009
  • jedi
    September 11, 2009
  • Kevin
    September 11, 2009
  • 3m masr
    December 1, 2009
  • rudy
    January 28, 2010
  • Dileep
    February 10, 2010
  • jeffery
    February 10, 2010
  • David
    March 14, 2010
  • me
    June 8, 2010
  • ????? ?? ????
    June 13, 2010
  • ScarLight
    August 12, 2010
  • Forex
    August 14, 2010
  • kathir
    September 3, 2010
  • cihip
    September 5, 2010
    • ?? ???
      February 25, 2012
  • Ciprian
    September 20, 2010
  • Amjad
    September 28, 2010
  • Shaun
    October 19, 2010
  • SMS
    October 19, 2010
  • Accident Claims
    November 4, 2010
  • Janes Oosthuizen
    November 7, 2010
  • niranjana
    November 16, 2010
  • aschwin
    November 29, 2010
  • Amrish
    December 4, 2010
  • Carl
    December 27, 2010
  • Ram
    December 28, 2010
  • zahir
    December 30, 2010
  • thupten
    January 11, 2011
  • martin slattery
    February 5, 2011
  • 3 mobile u
    February 11, 2011
  • Gareth
    February 15, 2011
  • Rammi
    February 18, 2011
  • Rammi
    February 18, 2011
  • John
    February 27, 2011
    • Tony
      February 9, 2012
  • upendra
    March 8, 2011
  • Ravi
    April 5, 2011
  • Theraisa K
    April 6, 2011
    • pbu
      April 6, 2011
  • Pierre FAY
    April 8, 2011
  • Dharani
    April 9, 2011
  • Truman Wanty
    April 11, 2011
  • web design company india
    April 28, 2011
  • jowan
    April 28, 2011
  • Nitish
    May 2, 2011
  • Brian Nicky
    May 2, 2011
  • ali
    May 13, 2011
  • siva
    May 19, 2011
  • JMDesign
    May 30, 2011
  • murali
    June 25, 2011
  • ??????
    July 12, 2011
  • alexander
    July 20, 2011
  • iletras
    July 25, 2011
  • roncero
    July 28, 2011
  • Gopesh
    July 30, 2011
  • laptop_seller
    September 8, 2011
  • Per
    October 11, 2011
  • James BEE
    October 11, 2011
  • minyatoor
    October 15, 2011
  • ashik
    October 23, 2011
  • radhe
    November 1, 2011
  • codename47
    December 15, 2011
  • Scott
    January 23, 2012
  • asha
    January 24, 2012
  • ???
    January 30, 2012
  • web design
    January 31, 2012
  • ???
    January 31, 2012
  • ???? ??? ??? ????
    February 4, 2012
  • Dog Grooming
    February 13, 2012
  • jewel
    March 1, 2012
  • Jake
    April 10, 2012
  • Fawwad
    May 30, 2012
  • ?????? ?????
    June 7, 2012
  • mTrang
    June 8, 2012
  • v
    June 13, 2012
  • Javaman59
    September 22, 2012
  • eremiya
    October 26, 2012
  • NAVEN
    December 10, 2012
  • Shenu
    May 7, 2013

Leave A Comment.






9 − = one

Please wrap all source codes with [code][/code] tags. Powered by