2013-03-19 104 views
0

我正在寻找如何编写使用Qunit框架单元测试用例unobstrusive JavaScript客户端验证的参考: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;   
}); 

回答