Comparing Fields and Customizing Error Messages with jQuery`s Validator Plug-in - Review: validating numeric values and file extensions
(Page 2 of 4 )
It’s possible that you still haven’t read the preceding tutorial, where I explained how to use the “number” and “accept” options provided by the Validator plug-in for checking numeric values and file extensions. Therefore, below I reintroduced the examples created in that article, which show a simple use of these arguments. Here they are:
(example on using the ‘number’ 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 the number 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,
email: true
},
age: {
required: true,
number: true
}
}
});
});
</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>Age <input type="text" name="age" class="required" /></p>
<p><input type="submit" value="Submit" /></p>
</form>
</body>
</html>
(example on using the ‘accept’ 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 the accept 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,
email: true
},
avatar: {
required: true,
accept: "jpg|gif|png"
}
}
});
});
</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>Avatar <input type="text" name="avatar" class="required" /></p>
<p><input class="submit" type="submit" value="Submit" /></p>
</form>
</body>
</html>
As you can see, the first code sample illustrates how to use the “number” option to check if a value entered in a specific form field is numeric or not, while the last example shows a simple usage of the “accept” argument for filtering some common graphic file extensions.
At this point, you hopefully grasped how the previous examples work, so it’s time to see how to utilize the capabilities of the Validator plug-in to determine whether the values assigned to two fields of an HTML form are the same.
If you wish to learn the full details of this process, then click on the link that appears below and read the next section.
Next: The equalTo argument >>
More JavaScript Articles
More By Alejandro Gervasio