jQuery Tools Validator - Require input in either of two fields

萝らか妹 提交于 2019-12-11 12:59:17

问题


I am using jQuery Tools Validator to ensure that my form fields are not blank as follows:

$(document).ready(function(){
  $.tools.validator.fn("#field1", function(input, value) {
    return value != '' ? true : {     
        en: "This field is required"
    };
  });

  $.tools.validator.fn("#field2", function(input, value) {
    return value != '' ? true : {     
        en: "This field is required"
    };
  });


  var form = $("#form").validator({ 
    position: 'bottom left', 
    offset: [5, 0],
    messageClass:'form-error',
    message: '<div><em/></div>' 
  }).attr('novalidate', 'novalidate');
});

I need users to enter text in either one of the two fields (or both) but I am not sure how to write a matcher for the two fields.


回答1:


If you want either or both, why not:

$.tools.validator.fn("#field1", function(input, value) {
    return value !== '' || $('#field2').val() !== '' ? true : {     
        en: "This field is required"
    };
});
//vice-versa for field2


来源:https://stackoverflow.com/questions/5935269/jquery-tools-validator-require-input-in-either-of-two-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!