jQuery validationEngine: Extended jQuery form validations
The jquery.validationEngine plugin for form validation is a good example of un-obtrusive validation. Its clean, neat, simple, beautiful UI, yet powerful validation plugin. This plugin is having most of the form fields validations that are commonly required in any project.
I have extended it for one of my projects by adding three functions to it.
See working example – http://www.navayan.com/register.php
I have added following three functions to core jquery.validationEngine plugin to extend the power of jquery form validation on the fly:
1. _maxListOptions() - this will check whether the limit of selecting the options from <select> dropdown list is not more than specified. 2. _minListOptions() - Similarly this function will prompt to select at least one option from <select> dropdown list. 3. _checkDuplicate() - This function will check the uniqueness of the two specified form fields
Above functions in details:
_maxListOptions: function(field, rules, i, options) {
var listItems = rules[i + 1];
var groupname = field.attr("name");
var groupSize = $("select[name='" + groupname + "'] option:selected").size();
if (groupSize > listItems) {
var rule = options.allrules.maxListOptions;
return rule.alertText + listItems + rule.alertText2;
}
},
_minListOptions: function(field, rules, i, options) {
var listItems = rules[i + 1];
var groupname = field.attr("name");
var groupSize = $("select[name='" + groupname + "'] option:selected").size();
if (groupSize < listItems) {
var rule = options.allrules.minListOptions;
return rule.alertText + listItems + rule.alertText2;
}
},
_checkDuplicate: function(field, rules, i, options) {
var equalsField = rules[i + 1];
if (field.attr('value') == $("#" + equalsField).attr('value'))
return options.allrules.checkDuplicate.alertText;
},
Adding method in _validateField() function:
case "maxListOptions":
errorMsg = methods._maxListOptions(field, rules, i, options);
field = $($("select[name='" + fieldName + "']"));
break;
case "minListOptions":
errorMsg = methods._minListOptions(field, rules, i, options);
field = $($("select[name='" + fieldName + "']"));
break;
case "checkDuplicate":
errorMsg = methods._checkDuplicate(field, rules, i, options);
break;
Setting rules for new methods with validation messages:
"maxListOptions": {
"regex": "none",
"alertText": "* Select max ",
"alertText2": " options only"
},
"minListOptions": {
"regex": "none",
"alertText": "* Select minimum ",
"alertText2": " option"
},
"checkDuplicate": {
"regex": "none",
"alertText": "* Duplicate entry"
},
Things that need to be present in the class attribute of form elements:
validate[required,minListOptions[1],maxListOptions[3]] validate[checkDuplicate[primary_email],custom[email]]
Following things that are required to work around:
BASIC HTML FORM:
<form id="form_contact" action="#" method="post"> <ul> <li>Occupation *</li> <li> <select class="validate[required,minListOptions[1],maxListOptions[3]]" multiple="multiple" id="occupation" name="occupation"> <option value="1">Accountant</option> <option value="2">Admin Officer</option> <option value="3">Air Hostess</option> <option value="4">Archaeologist</option> <option value="5">Architect</option> <option value="6">CEO</option> <option value="7">Charted Accountant</option> </select> <small>Select minimum one and maximum three occupations</small> </li> </ul> <ul> <li>Primary email</li> <li>someone@gmail.com <input type="hidden" value="someone@gmail.com" id="primary_email" name="primary_email"/></li> </ul> <ul> <li>Secondary email</li> <li><input type="text" class="validate[checkDuplicate[primary_email],custom[email]]" value="someone@yahoo.com" id="secondary_email" name="secondary_email"/></li> </ul> <ul> <li> </li> <li><input type="submit" value="Update" name="submit"></li> </ul> </form>
CSS
<link type="text/css" rel="stylesheet" media="all" href="http://cross-browser-reset-css-framework.googlecode.com/files/cbresetcss-1.4.css" /> <link type="text/css" rel="stylesheet" href="http://jquery-validatation-engine-extended.googlecode.com/files/validationEngine.jquery.css" />
JAVASCRIPTS
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery-validatation-engine-extended.googlecode.com/files/jquery.validationEngine.js"></script>
<script type="text/javascript" src="http://jquery-validatation-engine-extended.googlecode.com/files/jquery.validationEngine-en.js"></script>
<script type="text/javascript">
$(function(){
$('#form_contact').each(function(){
$(this).validationEngine();
});
});
</script>
Working example – http://www.navayan.com/register.php
Advertisement


Buddhist Matrimony (Buddhist Parinay)
Navayan.com
People’s Education Society
Hi
It’s great but not working on IE9……….
Have any idea ?
@Johnny – The validation plugin works just fine in IE9 as well. Did you cleared your browser’s cache and/or history? Also try Ctrl + F5 to reload javascripts. That should work without any issue.
Sorry , I am not acturally install IE9 . I just use IETEST to test validation plugin .,
IE8 works fine but IE9 not works in IETEST .
Is there IETEST problem ?
Thanks
@Johnny – It might be the problem with IETEST. I have tested on IE9 standard version from Microsoft itself. Also it works without any issue in different modes ie. IE7, IE8, IE9. So try on standard version.
Am in love with your validation Engine. I have the latest version. Working cool but … there is an issue , have been trying to fix it but
quite difficult.
I want to use validation engine on a form inside an overlay Modalbox called Boxy… I love boxy bcos its cool and nice….. but your validation Engine does not really work inside BOXY Modal window….
I manage to make it work but it doesn’t validate properly.
When i open a fresh page and open the Boxy Modal box containing form, it works but when i close the boxy modal box and re-open it the validation no longer works
… only works if i refresh page and reopen modal box, BOXY
This is great. How would I break up a large form into a tabbed interface? I am using jQuery UI but whatever works. Is it as simple as using multiple forms?
@shaggy – You can use the same plugin on multiple forms. You will only need to provide the form id for validation. For example:
$(‘#form_contact’).each( function() {$(this).validationEngine();
});
$(‘#form_personal’).each( function() {
$(this).validationEngine();
});
$(‘#form_survey’).each( function() {
$(this).validationEngine();
});
I have used it seven times with tabbed interface in my project http://www.navayan.com for user’s edit profile.
Please see my Cookie Based jQuery Tabs plugin for this. And it will not download the javascript for each tab so go ahead by using tabbed interface:
Position Absolute’s jquery field validation is passed when user didn’t yet enter information if the field input is watermarked by Watermark input plugin.
Position Absolute : http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
Watermark Input : http://digitalbush.com/projects/watermark-input-plugin/
Is it possible to change this behaviour?
For example,
Name : [ Enter your Username? ]
If user didn’t enter anything, it passes thru where as normal behaviour should be, it should block it.
@phpnewbie – I will check and post a comment
@phpnewbie – I don’t think you need watermark input plugin. You can do that behavior using jQuery’s ‘blur’ and ‘focus’ events.
Try following functions:
// GLOBAL FOCUS/BLURfunction isFocus(field, val){
if(field.value == val){field.value = '';}
}
function isBlur(field, val){
if(field.value == ''){field.value = val;}
}
$('#input_field_id').focus(function(){isFocus(this, 'First Name');});
$('#input_field_id').blur(function(){isBlur(this, 'First Name');});
Write CSS for font colors. And some basic validation rules to check the default focus/blur text in input field.
Is that what you were looking for?
Nice write up, I was just wondering, would you happen to know how I can make the inputs have a red border when the validation fails and then disappear when it passes. I have this so far which puts the border fine, but can’t figure out how to remove it when the fields are correct. Thanks
$(“#myForm”).bind(“jqv.form.result”, function(event , errorFound){
if(errorFound)$(“*[class*='validate'], select[class*='validate']“).addClass(“redBd”);
$(“input[class*='validate'], select[class*='validate']“).parentsUntil().addClass(“validation-error”);
})
Great work. I’ve just got one question. I have multiple buttons on my page and only want to validation to fire off on one of them. Is there a way to disable it on certain buttons?
@akismet – yes, it is possible. The validation engine looks for the ID of the form. You can have multiple buttons/links there on the same form. You will just need to change-and-revert the ID of form which you ‘want-to/not-want-to’ validate.
I hope this answers your question. If not then please share a link of your form page.