David
David

Reputation: 7303

jQuery Tools Validator - Require input in either of two fields

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.

Upvotes: 1

Views: 2468

Answers (1)

Zirak
Zirak

Reputation: 39808

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

Upvotes: 3

Related Questions