Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > JQuery Validation Tutorial

JQuery Validation Tutorial

This JQuery CSS validation tutorial explain how to do client-side validation using jQuery’s validation plugin. This is how the validation snapshot of the validation page. The link to download the code is at end of this tutorial.

snapshot

This jquery validation is very easy to use and has the good look and feel for the users in the webpage forms and it’s a powerful client Side Validation. The code samples are attached below

Files to be used in the JQuery Validation

  • css File (ValidationEngine.Jquery.css)
  • JQuery.min.js
  • JQuery.validationEngine-en.js
  • JQuery.validationEngine.js
  • Validation File – Target File

JQuery Configuration

First, you need to include the js files and css files as in the Target File

(samplevalidate.html).

<script type=”text/javascript” src=”jquery.min.js”></script>

<script type=”text/javascript” src=”jquery.validationEngine-en.js”></script>

<script type=”text/javascript” src=” jquery.validationEngine.js “>

And the following script should be in the target file as follows,

<script>

$(document).ready(function()

{

// SUCCESS AJAX CALL, replace “success: false,” by:

success : function() { callSuccessFunction() },

$(“#form”).validationEngine()

//$.validationEngine.loadValidation(“#date”)

//alert($(“#formID”).validationEngine({returnIsValid:true}))

//$.validationEngine.buildPrompt(“#date”,”This is an example”,”error”)

// Exterior prompt build example

// input prompt close example

//$.validationEngine.closePrompt(“.formError”,true)

// CLOSE ALL OPEN PROMPTS

});

</script>

Working Techniques:

Before proceeding with the files used in JQuery validation we must know the usage of the files.

Main HTML file (samplevalidate.html) – here we will be doing the JQuery validation

JS Files – Supporting files to do the validation using JQuery and the

js script to call the function in the target file

CSS File – The layout(size, colors, shape) is defined in the CSS

MAIN HTML FILE

The file is where we need to integrate the JQuery validation

The main Html file will have the following lines

<script type=”text/javascript” src=”jquery.min.js”></script>

<script type=”text/javascript” src=”jquery.validationEngine-en.js”></script>

<script type=”text/javascript” src=” jquery.validationEngine.js “>

<script>

$(document).ready(function()

{

// SUCCESS AJAX CALL, replace “success: false,” by:

success : function() { callSuccessFunction() },

$(“#form”).validationEngine()

//$.validationEngine.loadValidation(“#date”)

//alert($(“#formID”).validationEngine({returnIsValid:true}))

//$.validationEngine.buildPrompt(“#date”,”This is an example”,”error”)

// Exterior prompt build example     // input prompt close example

//$.validationEngine.closePrompt(“.formError”,true)    // CLOSE ALL OPEN PROMPTS

});

</script>

The form part which needs to be validated using JQuery can be something like the below

<form id=”form” method=”post” action=”#”>

<div id=”login”>

<table width=”350px” cellspacing=”15″ cellpadding=”5″ border=”0″ >

<tr>

<th width=”45%” align=”right”>Username</th>

<th width=”5%”>:</th>

<td><input class=”validate[requireds] text-input” id=”username” title=”username” size=”30″></td>

</tr>

<tr>

<th width=”45%”  align=”right”>Password</th>

<th width=”5%”>:</th>

<td><input class=”validate[requireds] text-input”  id=”password” title=”password” size=”30″></td>

</tr>

<tr>

<th id=”submit” colspan=”2″ valign=”top”></td>

<td align=”center”>

<input type=”submit” value=”submit” name=”submit” />

</td>

</tr>

<tr>

<th id=”submit” colspan=”2″ valign=”top”></td>

<td align=”center” valign=”top”>

<a href=”signup.php”><b>Sign up</b></a>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<a href=”forgotpassword.php”><b>Forgot Password</b></a>

</td>

</tr>

</table>

</div>

</form>

The necessary controls (textbox,checkbox,radio,select,etc) should have the attributes of class, id and title…

Class:

The class field is useful to specify the type of validation. Like the content data is necessary to validate or optional and is number or is email or required character count. These are possible to validate by the jquery/css validation..

Id:

The id attribute is useful to access value from required controls.

Title:

The title attribute is useful to show the user needed error messages.

JS Files

1.JQuery.Min.js

Download  the code sample –> jquery.min.js

http://code.google.com/p/jqueryjs/downloads/list?can=2&q=jquery.min.js

2.  JQuery.validationEngine-en.js

Download  the code sample –> jquery.validationEngine-en.js

This js is used to define the alert messages. We can customize them as per our need.

(function($) {

$.fn.validationEngineLanguage = function() {};

$.validationEngineLanguage = {

newLang: function() {

$.validationEngineLanguage.allRules =            {“required”:{                                       // Add your regex rules here, you can take telephone as an example

“regex”:”none”,

“alertText”:”* This field is required”,

“alertTextCheckboxMultiple”:”* Please select an option”,

“alertTextCheckboxe”:”* This checkbox is required”},

“requireds”:{                                      // Add your regex rules here, you can take telephone as an example

“regex”:”none”,

“alertText”:”",

“alertTextCheckboxMultiple”:”",

“alertTextCheckboxe”:”"},

“length”:{

“regex”:”none”,

“alertText”:”*Between “,

“alertText2″:” and “,

“alertText3″: ” characters allowed”},

“maxCheckbox”:{

“regex”:”none”,

“alertText”:”* Checks allowed Exceeded”},

“minCheckbox”:{

“regex”:”none”,

“alertText”:”* Please select “,

“alertText2″:” options”},

“confirm”:{

“regex”:”none”,

“alertText”:”* Your field is not matching”},

“telephone”:{

“regex”:”/^[0-9\-\(\)\ ]+$/”,

“alertText”:”* Invalid phone number”},

“email”:{

“regex”:”/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/”,

“alertText”:”* Invalid email address”},

“date”:{

“regex”:”/^[0-9]{4}\-\[0-9]{1,2}\-\[0-9]{1,2}$/”,

“alertText”:”* Invalid date, must be in YYYY-MM-DD format”},

“onlyNumber”:{

“regex”:”/^[0-9\ ]+$/”,

“alertText”:”* Numbers only”},

“noSpecialCaracters”:{

“regex”:”/^[0-9a-zA-Z]+$/”,

“alertText”:”* No special caracters allowed”},

“ajaxUser”:{

“file”:”validateUser.php”,

“extraData”:”name=eric”,

“alertTextOk”:”* This user is available”,

“alertTextLoad”:”* Loading, please wait”,

“alertText”:”* This user is already taken”},

“ajaxName”:{

“file”:”validateUser.php”,

“alertText”:”* This name is already taken”,

“alertTextOk”:”* This name is available”,

“alertTextLoad”:”* Loading, please wait”},

“onlyLetter”:{

“regex”:”/^[a-zA-Z\ \']+$/”,

“alertText”:”* Letters only”}

}

}

}

})(jQuery);

$(document).ready(function() {

$.validationEngineLanguage.newLang()

});

3. JQuery.validationEngine.js

Download  the code sample –> jquery.validationEngine.js

http://www.position-relative.net/creation/formValidator/js/jquery.validationEngine.js

This js files is used for the validation of the form

CSS FILE:

The styles for the message are defined in this css.

.formError {

position:absolute;

top:300px; left:300px;

padding-bottom:13px;

display:block;

z-index:5000;

}

#debugMode{

background:#000;

position:fixed;

width:100%; height:200px;

top:0; left:0;

overflow:scroll;

opacity:0.8;

display:block;

padding:10px;

color:#fff;

font-size:14px;

z-index:100000;

}

.ajaxSubmit{ padding:20px; background:#55ea55;border:1px solid #999;display:none}

.formError .formErrorContent {

width:100%;

background:#ee0101;

color:#fff;

width:150px;

font-family:tahoma;

font-size:11px;

border:2px solid #ddd;

box-shadow: 0px 0px 6px #000;

-moz-box-shadow: 0px 0px 6px #000;

-webkit-box-shadow: 0px 0px 6px #000;

padding:4px 10px 4px 10px;

border-radius: 6px;

-moz-border-radius: 6px;

-webkit-border-radius: 6px;}

.greenPopup .formErrorContent {background:#33be40;}

.blackPopup .formErrorContent {background:#393939;color:#FFF;}

.formError .formErrorArrow{

position:absolute;

bottom:0;left:20px;

width:15px; height:15px;

z-index:5001;

}

.formError .formErrorArrowBottom{top:0;margin:-6px;}

.formError .formErrorArrow div{

border-left:2px solid #ddd;

border-right:2px solid #ddd;

box-shadow: 0px 2px 3px #444;

-moz-box-shadow: 0px 2px 3px #444;

-webkit-box-shadow: 0px 2px 3px #444;

font-size:0px; height:1px; background:#ee0101;margin:0 auto;line-height:0px; font-size:0px; display:block;

}

.formError .formErrorArrowBottom div{

box-shadow: none;

-moz-box-shadow: none;

-webkit-box-shadow: none;

}

.greenPopup .formErrorArrow div{background:#33be40;}

.blackPopup .formErrorArrow div{background:#393939;color:#FFF;}

.formError .formErrorArrow .line10{width:15px;border:none;}

.formError .formErrorArrow .line9{width:13px;border:none;}

.formError .formErrorArrow .line8{width:11px;}

.formError .formErrorArrow .line7{width:9px;}

.formError .formErrorArrow .line6{width:7px;}

.formError .formErrorArrow .line5{width:5px;}

.formError .formErrorArrow .line4{width:3px;}

.formError .formErrorArrow .line3{width:1px;

border-left:2px solid #ddd;

border-right:2px solid #ddd;

border-bottom:0px solid #ddd;}

.formError .formErrorArrow .line2{width:3px;border:none;background:#ddd;}

.formError .formErrorArrow .line1{width:1px;border:none;background:#ddd;}

Customizing the JQuery validation:

We can customize the appearance of JQuery validation by altering the css file

Some of the Examples for the form controls,

Example to the validation

E.x : 1

Text Box Value Validation:

<input type=”text” id=”first_name” title=”first name”>

E.x : 2

Email Validation:

<input type=”text” title=”email” >

E.x : 3

Select Box Validation:

<select class=”validate[requireds]” title=”file type”>

Class :

The class field is useful to specify the type of validation. Like the content data is necessary to validate or optional and is number or is email or required character count. These are possible to validate by the jquery/css validation..

Id:

The id attribute is useful to access value from required controls.

Title :

The title attribute is useful to show the user needed error messages

Conclusion:

JQuery validation is a good alternate for simple JavaScript form client side validation technique. And it’s easy to customize as per the requirement.

Download the sample code and all the files used in this example:   formValidator.zip

For demo please check the link  http://www.position-relative.net/creation/formValidator/

Download the zip:   formValidator.zipDownload the zip:   formValidator.zip

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
Categories: Programming / tutorials Tags: , , ,
  1. Krunal Patel
    November 16th, 2011 at 01:37 | #1

    Its great .. But i want to use in my asp.net web site . How can i use this code in my asp.net site with multiple form .

  2. November 9th, 2011 at 06:44 | #2

    ide and seek form validation using jQuery
    http://wp.me/p1Wsov-2l

  3. Selvin
    October 31st, 2011 at 08:27 | #3

    Will this work in Struts tag? “class” attribute is not supported in “” tag…

  4. Mart
    January 17th, 2011 at 13:11 | #4

    The validateUser doesn’t seem to work. There seems to be a general problem with this everywhere I turn on the web.
    How can you successfully invoke a user check that for example will not allow a specific name.(e.g. already in use)

  5. Yeswanth
    January 12th, 2011 at 01:48 | #5

    a million thanks for this tutorial.

  6. January 8th, 2011 at 14:09 | #6

    all buttons on the form are working for validations,which should not happen. only one button
    should perform the validations

  7. November 7th, 2010 at 13:25 | #7

    Thanks for the great script!

  8. Amit
    September 3rd, 2010 at 06:33 | #8

    Hi,
    I’ve a textarea on my page and i want to have validation only on form submit. i don’t wan’t to validate textarea on onBlur. Can anybody help/guide or suggest thing.

  9. August 21st, 2010 at 19:40 | #9

    Not sure of that. Need to find out.

  10. Benito
    August 21st, 2010 at 17:20 | #10

    Great tutorial. Totally worked for me. I was wondering, does jquery have any captcha-type validation built in too?

  11. May 3rd, 2010 at 13:20 | #11

    you need to write more and moree, love your stuff, bookmarked

  1. April 18th, 2010 at 12:54 | #1
  2. April 18th, 2010 at 12:55 | #2
  3. April 18th, 2010 at 15:50 | #3
  4. April 24th, 2010 at 20:19 | #4
  5. April 29th, 2010 at 03:58 | #5
Get Adobe Flash playerPlugin by wpburn.com wordpress themes