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.
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>
Styling form and messages with CSS
<link rel="stylesheet" media="all" href="http://cross-browser-reset-css-framework.googlecode.com/files/cbresetcss-1.4.css">
<link rel="stylesheet" href="http://jquery-validatation-engine-extended.googlecode.com/files/validationEngine.jquery.css">
Javascripts that are needed
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script src="http://jquery-validatation-engine-extended.googlecode.com/files/jquery.validationEngine.js"></script>
<script src="http://jquery-validatation-engine-extended.googlecode.com/files/jquery.validationEngine-en.js"></script>
<script>
$(function(){
$('#form_contact').each(function(){
$(this).validationEngine();
});
});
</script>
July 20, 2013 at 3:09 pm
Hi. I used this code on my listbox control in my form. It works well. I want another solution for my list box control. i.e. I need to submit only 5 options by my listbox . If user try to submit morethan 5 options while press the submit button, then the jquery validation need to fire and need to display validation error.
July 21, 2013 at 11:55 pm
Just use
minListOptions[1],maxListOptions[5]
in select field’s ‘class’ attribute.July 22, 2013 at 4:18 pm
Hi Amol Nirmala Waman,
I want another solution, not this. I understand about that. I used 2 listboxes. First list box is used to select options and second list box is used to submit options. Your solution is used to first list box to select max and min options validation.I need validation to second list box. Selected options is no need to considered in second listbox. In the second list box options may be select or not. I submit second list box options. Just I want calculate options in the second list box with or without selected options.I need to submit max 5, min1 options. Actyally your code is worked at selected options. But I want another.
Thanks & Regards
Ram
July 22, 2013 at 4:20 pm
sorry , one mistake in my line.
Just I want calculate options in the second list box with and without selected options.
July 22, 2013 at 6:48 pm
Hi,
I used this var groupSize = $(“select[name='” + groupname + “‘] option”).size(); instead of var groupSize = $(“select[name='” + groupname + “‘] option:selected”).size();.
It works fine. Thank you
July 24, 2013 at 11:11 am
Hi it has minor issue. Please show my report and try to solve. I give all information about the issue.
Jquery validation engine problem: listbox required field validation
Hi I used jquery validation engine for form controls validation purpose. It works well at all controls. But It is partially worked on listbox control. I used 2 listboxes in my form. Because I
provide user to choose more than one options and remove users options. For this purpose I used 2 lisboxes. User can select options in the first listbox and pass to second listbox. Actally
listbox validation is needed on second listbox. Second listbox options are needed to submit. I also provide max list options to 5. It works well. I also added required field validation to
second listbox. But It does not work well. every time I needed to select min one option when I submit options. If not any option select in the second listbox then the required field validation
error is fired. I didnt understand why the required field is fired when the data is occured in the second listbox. I am new to jquery.
case “maxList”:
errorMsg = methods._maxList(field, rules, i, options);
field = $($(“select[name='” + fieldName + “‘]”));
break;
_maxList: function (field, rules, i, options) {
var listItems = rules[i + 1];
var groupname = field.attr(“name”);
var groupSize = $(“select[name='” + groupname + “‘] option”).size();
//alert(“HI” + groupSize);
if (groupSize > listItems) {
var rule = options.allrules.maxList;
return rule.alertText + listItems + rule.alertText2;
}
},
“maxList”: {
“regex”: “none”,
“alertText”: “* Select max “,
“alertText2″: ” subjects only”
},
I want to upload my error image. But I could not find the image upload option.
Thank u
July 24, 2013 at 5:28 pm
Hi Amol Nirmala Waman,
I have gotten solution of required field validation issue on second listbox control.
I have another issue on aspx radio button controls required field validation controls.
I used jquery validation engine. Required field validation is not working on Aspx radio button control. It works fine on html radio button control. My code is given below.
Issue: Jquery validation engine radio button required field validation error on aspx radio button control
Code:
Male
Female