我正在寻找有关如何使用 Qunit 框架编写单元测试用例以进行不引人注目的 javascript 客户端验证的参考:
jQuery.validator.addMethod('requiredifnotempty',
function(value, element, parameters) {
var otherPropertyId = '#' + parameters['otherproperty'];
// get the actual value of the other control
var otherControl = $(otherPropertyId);
var controlType = otherControl.prop('type');
var otherValue = otherControl.val();
if (controlType === 'checkbox') {
otherValue = otherControl.prop("checked").toString();
}
if (otherValue.trim().length > 0) {
return $.validator.methods.required.call(this, value, element, parameters);
}
return true;
}
);
jQuery.validator.unobtrusive.adapters.add(
'requiredifnotempty',
['otherproperty'],
function (options) {
options.rules['requiredifnotempty'] = {
otherproperty: options.params['otherproperty']
};
options.messages['requiredifnotempty'] = options.message;
});