1

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...

2
  • what do you want? i dont understand your question Commented Sep 28, 2012 at 10:41
  • i want the delivery_city: {zipcitycombination:[$('select[name="amsdelivery_country"]').val(),$('input#delivery_zip').val()]} to use the entered country and zip values and not the empty values Commented Sep 28, 2012 at 10:44

1 Answer 1

1

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:

var country = $(params[0]).val();
var zip = $(params[1]).val();

And configure validate like this:

$("#commentForm").validate({

    rules: {
        delivery_city: {
            zipcitycombination:['select[name="amsdelivery_country"]','input#delivery_zip']
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.