2013-03-19 120 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;   
}); 

回答