OK... so in your examples, the field is never blank in either form. Either it has a placeholder value, or an attempt at an email address. The whole point of this.optional(element)
is to immediately return true if the element is blank AND it is not required.
So if you had these two methods:
jQuery.validator.addMethod("BOB", function (value, element) {
return this.optional(element) ||
element.value === 'BOB';
}, 'You did not enter BOB');
jQuery.validator.addMethod("mustbeBOB", function (value, element) {
return element.value === 'BOB';
}, 'You did not enter BOB');
Adding a class of BOB required
would be the same as entering a class of mustbeBOB
. Compare that to having a class of BOB
which would allow for a blank or "BOB", vs a class of mustbeBOB
which will only pass validation with a value of BOB
, blank would fail. Does that make more sense?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…