Home arrow JavaScript arrow Page 3 - Validating Ranges, Emails, and URLs with jQuery`s Validator Plug-in
JAVASCRIPT

Validating Ranges, Emails, and URLs with jQuery`s Validator Plug-in


In this fourth part of a seven-part series on the jQuery Validator plug-in, I explain how to use the “range,” “email” and “url” options provided for checking numeric ranges, URLs and email addresses. Using these arguments is an intuitive process that can be learned in a very short time.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
November 05, 2009
TABLE OF CONTENTS:
  1. · Validating Ranges, Emails, and URLs with jQuery`s Validator Plug-in
  2. · Review: the rangelength, min and max options
  3. · Introducing the range argument
  4. · Validating email addresses and URLs with the email and url options

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Validating Ranges, Emails, and URLs with jQuery`s Validator Plug-in - Introducing the range argument
(Page 3 of 4 )

As I mentioned in the introduction, the Validator plug-in allows you to check very easily if data entered in a form field is within a specified numeric range, by using yet another handy option called “range.” The most significant advantage in utilizing this argument is that it permits you to achieve the same result when working with the “min” and “max” options at the same time, but in a single step.

Now that you hopefully have a vague idea of how the “range” argument functions, let me show you a concrete example of it in action. Take a look at the following code sample, please:

<!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 the range argument</title>

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

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

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

<script>

$(document).ready(function(){

$("#sampleform").validate({

rules: {

day: {

required: true,

range: [1, 31]

},

month: {

required: true,

range: [1, 12]

},

year: {

required: true,

range: [1920, 2008]

}

}

});

});

</script>

</head>

<body>

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

<p>Day <input name="day" class="required" /></p>

<p>Month <input name="month" class="required" /></p>

<p>Year <input name="year" class="required" /></p>

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

</form>

</body>

</html>

As you can see, the above example demonstrates how to validate numeric ranges via the aforementioned “range” option. Since in this particular case, the data collected through the web form must be supplied in the form of day, month and year respectively, the “range” argument is very useful for checking whether or not these values fall in the correct range. If any field is found to be invalid after submitting the form, then the plug-in should react by displaying a few error messages similar to the ones shown below:

Now, do you realize how easy it is to validate numeric ranges with the Validator plug-in? I bet you do. However, as I expressed in the beginning, the plug-in also has the ability to check email addresses and URLs as well. Thus, in the final section of this tutorial I’m going to show you how to accomplish these tasks with a couple of simple arguments.

Now, click on the link below and read the following segment.


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