How do you create a simple, custom rule using the JQuery Validate plugin (using addMethod) that doesn't use a regex?
For example, what function would create a rule that validates only if at lease one of a group of checkboxes is checked?
How do you create a simple, custom rule using the JQuery Validate plugin (using addMethod) that doesn't use a regex?
For example, what function would create a rule that validates only if at lease one of a group of checkboxes is checked?
You can create a simple rule by doing something like this:
jQuery.validator.addMethod("greaterThanZero", function(value, element) {
return this.optional(element) || (parseFloat(value) > 0);
}, "* Amount must be greater than zero");
And then applying this like so:
$('validatorElement').validate({
rules : {
amount : { greaterThanZero : true }
}
});
Just change the contents of the 'addMethod' to validate your checkboxes.
Hi edward, I need this functionality as well, but my code isn't executing... Can you post a more complete example of you code?
I don't know where to write the $.validator.addMethod, should that be outside of the form.validate code?
I currently have:
jQuery.validator.addMethod("at_least_one_cat_selected", function(value, element) { var the_list_array = $("ul#list_all_categories li input:checked"); return the_list_array.length > 0; }, "Please specify the correct domain for your documents");
and then inside the validation call:
rules: { list_all_categories: {at_least_one_cat_selected: true} },
but that isn't working. Please note: list_all_categories is the name of the that contains all checkboxes?
$(document).ready(function(){
$.validator.addMethod("uniqueUserName", function(value, element) {
$.ajax({
type: "POST",
url: "http://"+location.host+"/checkUser.php",
data: "checkUsername="+value,
dataType:"html",
success: function(msg)
{
// if the user exists, it returns a string "true"
if(msg == "true")
return false; // already exists
return true; // username is free to use
}
})
}, "Username is Already Taken");
$("#regFormPart1").validate({
username: {
required: true,
minlength: 8,
uniqueUserName: true
},
messages: {
username: {
required: "Username is required",
minlength: "Username must be at least 8 cheractors",
uniqueUserName: "This Username is taken already"
}
}
});
########################
## In validation Rules #
########################
regemail: {
required: true,
email: true,
remote: {
url: "checkmail.php"
}
},
#####################
## In message Rules #
#####################
regemail: {
required: "Email is required",
email: "Invalid Format",
remote: ""
}
###########################
## Modify Jquery Validate #
###########################
#Search For:
var valid = response === true;
if ( valid ) {
#Change To:
var valid = response.data;
if ( !valid ) {
valid = true;
#Search For
} else {
var errors = {};
#Change To
} else {
valid = false;
var errors = {};
#Search For:
errors[element.id] = previous.message = response || validator.defaultMessage( element, "remote" );
#Change To:
errors[element.id] = response.data || validator.defaultMessage( element, "remote" );
#PHP Returns JSON return ($m == $m) ? false : 'Email Already Registered'