Home arrow JavaScript arrow Page 2 - Using Rangelength, Min and Max with the Validator jQuery Plug-in
JAVASCRIPT

Using Rangelength, Min and Max with the Validator jQuery Plug-in


When it comes to validating web forms in the client, one of the most effective and elegant applications that can be used nowadays is the Validator jQuery plug-in. It's a compact and powerful piece of software written by Jörn Zaefferer that allows you to check data collected through online forms with ease in a truly unobtrusive way.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
October 30, 2009
TABLE OF CONTENTS:
  1. · Using Rangelength, Min and Max with the Validator jQuery Plug-in
  2. · Review: the minlength and maxlength options
  3. · Checking length ranges with the rangelength option
  4. · Checking numeric ranges with the min and max options

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using Rangelength, Min and Max with the Validator jQuery Plug-in - Review: the minlength and maxlength options
(Page 2 of 4 )

As a prelude to exploring the “rangelength” option that was mentioned at the beginning, I'm going to reintroduce the couple of hands-on examples developed in the preceding article. They illustrated how to work with the “minlength” and “maxlength” arguments, first independently and then in combination.

That being said, here’s the full source code corresponding to these introductory examples:

(example on using the ‘minlength’ option)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Example on validating web form with minlength argument</title>

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

<script src="jquery.validate.js" type="text/javascript"></script>

<script>

$(document).ready(function(){

$("#sampleform").validate({

rules: {

fname: {

required: true,

minlength: 2

},

lname: {

required: true,

minlength: 2

},

email: {

required: true,

minlength: 6

}

}

});

});

</script>

</head>

<body>

<form id="sampleform" method="post" action="process_form.php">

<p>First Name <input type="text" name="fname" class="required" /></p>

<p>Last Name <input type="text" name="lname" class="required" /></p>

<p>Email Address <input type="text" name="email" class="required" /></p>

<p><input type="submit" value="Submit" /></p>

</form>

</body>

</html>

(example on using the ‘maxlength’ option)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Example on validating web form with maxlength argument</title>

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

<script src="jquery.validate.js" type="text/javascript"></script>

<script>

$(document).ready(function(){

$("#sampleform").validate({

rules: {

fname: {

required: true,

maxlength: 5

},

lname: {

required: true,

maxlength: 5

},

email: {

required: true,

maxlength: 8

}

}

});

});

</script>

</head>

<body>

<form id="sampleform" method="post" action="process_form.php">

<p>First Name <input type="text" name="fname" class="required" /></p>

<p>Last Name <input type="text" name="lname" class="required" /></p>

<p>Email Address <input type="text" name="email" class="required" /></p>

<p><input type="submit" value="Submit" /></p>

</form>

</body>

</html>

(example on using the ‘minlenth’ and ‘maxlength’ options combined)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Example on validating web form with minlength/maxlength argument</title>

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

<script src="jquery.validate.js" type="text/javascript"></script>

<script>

$(document).ready(function(){

$("#sampleform").validate({

rules: {

fname: {

required: true,

minlength: 2,

maxlength: 5

},

lname: {

required: true,

minlength: 2,

maxlength: 5

},

email: {

required: true,

minlength: 2,

maxlength: 8

}

}

});

});

</script>

</head>

<body>

<form id="sampleform" method="post" action="process_form.php">

<p>First Name <input type="text" name="fname" class="required" /></p>

<p>Last Name <input type="text" name="lname" class="required" /></p>

<p>Email Address <input type="text" name="email" class="required" /></p>

<p><input type="submit" value="Submit" /></p>

</form>

</body>

</html>

As you might have realized, understanding how to use the “minlength” and “maxlength” arguments for checking length ranges on data submitted through a web form is indeed a no-brainer process. In the first code sample, minimum length rules have been assigned to all of the fields of the selected online form, while in the second example the same process has been followed using the “maxlength” argument.

Finally, the last code sample shows how to use both options simultaneously, and due to its simplicity I’m not going to spend more time explaining how it works.

So far, so good. At this point, you hopefully recalled how to check for length ranges on HTML forms using the “minlength” and “maxlength” parameters together. Fortunately, the Validator plug-in provides yet another option that permits you to accomplish this in one single step via the “rangelength” argument. This is the one we will cover in depth in the following section. 

Therefore, to learn more on how to use this brand new validation option, click on the link below and read the next few lines.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials