开发者

issue with jquery validation plugin and checking one of 2 fields is populated

开发者 https://www.devze.com 2023-03-27 04:09 出处:网络
I have two fields (mobile and telephone) which are both being validated by the Validation plugin for jquery although Im struggling to get it working..

I have two fields (mobile and telephone) which are both being validated by the Validation plugin for jquery although Im struggling to get it working..

One of the fields must be populated although Im failing to successfully validation that checks on of the fields and it that has a value to allow t开发者_如何学Pythonhe other field to pass validation, even though its empty.

Is there an easy way to do this? I've tried add my own rules but I cant get it working :(

many thanks


required can take a callback function to determine if the validation should apply, so something like this should work:

$("#myform").validate({
  rules: {
    attr1: {
      required: function(el) {
        return $("#attr2").val();
      }
    },
    attr2: {
      required: function(el) {
        return $("#attr1").val();
      }
    }
  }
});

Tweaking the functions as necessary.

Edit: relevant docs link http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

0

精彩评论

暂无评论...
验证码 换一张
取 消