Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.validate: Multiple Remote Rules

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!

like image 830
mpmp Avatar asked May 24 '26 07:05

mpmp


1 Answers

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;
        }
     );
like image 86
Erez Rabih Avatar answered May 27 '26 16:05

Erez Rabih



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!