In our project we are using ValidationEngine, and we don't have the ability to replace it with another plug-in.
Our form has ten inputs and some of them are optional, but at least one of the optional fields must be included.
So, how do you validate inputs in this case?
Sample:
form
input1
input2
intpu3
input1: required
At least one of input2 and input3 must be present -- if both are empty validation should fail.
valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
¶jQuery Validation PluginThe plugin comes bundled with a useful set of validation methods, including URL and email validation, while providing an API to write your own methods. All bundled methods come with default error messages in english and translations into 37 other languages.
jQuery validation engine is a Javascript plugin aimed at the validation of form fields in the browser (IE 6-8, Chrome, Firefox, Safari, Opera 10). The plugin provides visually appealing prompts that grab user attention on the subject matter.
The validation engine now supports group Validation.Go to jQuery form validation plugin @ github and ake a look at groupRequired
. The syntax looks something like this.
<input value="" class="validate[groupRequired[payments]]" type="text" name="creditcard" id="creditcard" />
<input class="validate[groupRequired[payments]]" type="text" id="paypal" name="paypal"/>
It appears that the ValidationEngine plugin allows you to specify a validation rule which uses a custom function to determine validity.
If you add the validate class on input1
like so...
<input id="input1" class="validate[required,funcCall[myValidationFunction]]" />
Then ValidationEngine will use the following function. You can pretty much put any kind of logic in there. If I read your scenario correctly, this should accomplish what you're after.
function myValidationFunction() {
var input1 = $.trim($("#input1").val());
var input2 = $.trim($("#input2").val());
var input3 = $.trim($("#input3").val());
if (input1.length == 0) {
return "input1 is required";
}
if (input2.length == 0 && input3.length == 0) {
return "Either input2 or input3 is required";
}
// We don't need to return anything if there is no error.
}
Here's a link to the _funcCall
function in the source code:
https://github.com/posabsolute/jQuery-Validation-Engine/blob/master/js/jquery.validationEngine.js#L574
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With