jquery.validate: Multiple Remote Rules

jquery, validation

Solution

First I will say that as far as I know you have to nest the validation methods under a certain attribute - you can't just put them on global scope. This would make your code look like this:

$('#form').validate({
   rules:{
      attribute1: {
        remote_check_1: true,
        remote_check_2: true
      }
   },
    messages:{
      attribute1:{
        remote_check_1: 'Error1',
        remote_check_2: 'Error2'
      }
    }

});

I don't know if you can put the both under the rules object and raise different error messages for them.

What you can do is create two custom validation functions, each with different name. The result would look like:

$.validator.addMethod(
    "remote_check_1",
    function(value, element) {
      res = $.ajax({url: 'url1.php', data: { attribute1: value }, dataType: 'json'});
    return res;
    }
 );



$.validator.addMethod(
        "remote_check_2",
        function(value, element) {
          res = $.ajax({url: 'url2.php', data: { attribute1: value }, dataType: 'json'});
        return res;
        }
     );

Problem

I plan to validate a single text field with two remote rules. More like this. ``` $('#form').validate({ rules:{ remote: 'url1.php', remote: 'url2.php' }, messages:{ remote: 'Error1', remote: 'Error2' } }); ``` Is this possible? I would want separate error messages for the two remote rules. Hope to get some help!

Original source