I’m trying to add a custom validation method to the jquery-validation plugin. With given countrycode, zip and city, i call a php-script via ajax to check if this combination exists and if so, it gives back a “valid”.
So my added method looks like this:
jQuery.validator.addMethod("zipcitycombination", function(city, element, params) {
//function check zip and city
var country = params[0];
var zip = params[1];
$.ajax({
type: 'GET',
url: '/?eID=tx_zipcity',
async: false,
data: 'country=' + country + '&zip=' + zip+ '&city=' + city,
success: function(data){
response = ( data == 'valid' ) ? true : false;
}
})
return response;
}, "Please check your combination");
And now i try to use the validation like this:
$("#commentForm").validate({
rules: {
delivery_city: {
zipcitycombination:[$('select[name="amsdelivery_country"]').val(),$('input#delivery_zip').val()]
}
}
So everything works fine, except the zipcitycombination:
[$(‘select[name=”delivery_country”]’).val(),$(‘input#delivery_zip’).val()],
because it uses the empty values and not the values from the filled out input fields. Setting the values already in the html-file, the validation works as expected…
Assuming that your reason for not just extracting the values in the validator method is that you want to make the method generic, such that you can use it with different forms where the names/ids differ, I suggest you change the parameters to the validation method to be selectors instead of values, such as:
And configure
validatelike this: