Home arrow JavaScript arrow Page 2 - 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 - Review: checking web forms with the Validator jQuery plug-in
(Page 2 of 4 )

Before digging deeper into the use of the “minlength” and “maxlength” arguments, I’m going to spend a few moments reintroducing the examples developed in the previous article of the series. They demonstrated how to use the Validator plug-in to do a basic check of data entered into a simple HTML form.

That being said, here’s how these introductory examples looked originally:

(example on using the ‘validate()’ method to check all the fields of the targeted web form)

<!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 simple web form</title>

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

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

<script>

$(document).ready(function(){

$("#sampleform").validate();

});

</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 ‘validate()’ method to check only for certain fields of the targeted web form)

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

}

}

});

});

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

Undeniably, the two examples listed above are easy to follow. As you can see, in the first case the “validate()” has been called with no input arguments, therefore all of the fields included in the targeted form will be properly checked before the corresponding submission. On the other hand, the last example is a bit more complex, since it passes an extra “rules” parameter to the method to validate only the fields corresponding to the “First Name” and “Last Name” entries of the form. In addition, the data entered in these fields must be at least two characters long, due to the addition of the “minlength” option. Simple and also pretty effective, right?

Now that you hopefully recalled how to utilize the Validator plug-in for checking at a very basic level the data collected through a sample contact form, it’s time to explore some other features that come packaged with this plug-in. As I anticipated in the introduction, and as was demonstrated in the example that you just saw, it’s possible to specify a "minlength” option for each field of the form being validated. However, it’d be really helpful to clarify the use of this argument a bit further, as well as the use of its counterpart, “maxlength.”

Thus, in the section to come I’m going to set up another code sample for you, to give you a clear idea of how to perform more strict validation with the plug-in. As usual, to learn how this example will be developed, please click on the link below and keep reading.


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 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials