Home arrow JavaScript arrow Page 3 - Using Minlength and Maxlength with the Validator jQuery Plug-in
JAVASCRIPT

Using Minlength and Maxlength with the Validator jQuery Plug-in


If you don't know how to make your web site check web forms in the client, it's time you learned about the Validator jQuery plug-in. It's a compact and powerful application based on the popular jQuery library that will let you perform all sorts of clever validations on online forms in a truly no-brainer fashion, without spending endless hours coding complex JavaScript checking functions to check user-supplied data. This is the second part of a seven-part series that examines this plug-in in detail.

Author Info:
By: Alejandro Gervasio
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
October 29, 2009
TABLE OF CONTENTS:
  1. · Using Minlength and Maxlength with the Validator jQuery Plug-in
  2. · Review: checking web forms with the Validator jQuery plug-in
  3. · Specifying minimum lengths for all fields in a form
  4. · Specifying maximum lengths for all fields in a web form

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using Minlength and Maxlength with the Validator jQuery Plug-in - Specifying minimum lengths for all fields in a form
(Page 3 of 4 )

In the previous segment, I discussed the use of the “minlength” option to specify that certain fields of the targeted form should be at least two characters long. It's extremely simple to extend the use of this argument to all the fields, thus performing a more strict validation on user-submitted data.

So, say for instance that the email address of the HTML form coded previously now will be a required field, and a value of at least six characters long must be entered in it. How can this be accomplished? Well, the following example should answer this question for you. Check it out:

<!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>

It’s hard to believe, but that’s all the JavaScript code required to validate all of the fields of the above HTML form, which in this specific case must fit a minimum length as well. As you can see, thanks to the functionality given by the “minlength” option, it is pretty simple to define validation rules like these for any given web form. To complement the above example, here’s a screen capture that shows how well the Validator plug-in works when it detects an offending field:

Pretty good, huh? Not only does the plug-in permit us to validate fields in a stricter manner, but this process can be done in a truly unobtrusive fashion. You get double the benefits!

Having explained how the “minlength” option can be used to require minimum lengths for certain fields of a selected HTML form, it’s time to see how its counterpart “maxlength” argument can be utilized for specifying a maximum number of characters for those fields.

This particular situation will be represented with another example that will be an appropriate conclusion for this tutorial. Thus, to see how the “maxlength” option can be employed in a concrete case, please 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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials