3

1 つのテキスト フィールドを 2 つのリモート ルールで検証する予定です。もっとこう。

$('#form').validate({
   rules:{
      remote: 'url1.php',
      remote: 'url2.php'
   },
   messages:{
      remote: 'Error1',
      remote: 'Error2'
   }
});

これは可能ですか?2 つのリモート ルールに個別のエラー メッセージが必要です。

助けていただければ幸いです。

4

3 に答える 3

3

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;
        }
     );
于 2012-11-08T08:13:53.327 に答える
0

jquery.validation は、単一フィールドでの複数のリモート検証をサポートしていません。

ただし、サーバー側で検証ハンドラーをマージすることにより、2 つのリモート検証をマージできます。

于 2015-07-08T11:10:38.157 に答える