2011-10-03 130 views
0

我有以下幾點:jQuery的添加自定義的驗證

$('#qForm').validate({ 
    rules: { 

       answerYesNo_1: { required:true } , 

       answerYesNoText_1: { required: function(element) { 
             return $('#answerYesNo_1').val() == 'yes'; 
             } 
       , isMoney: true }, 
       answerYesNo_2: { required:true } , 

       answerYesNoText_2: { required: function(element) { 
             return $('#answerYesNo_2').val() == 'yes'; 
             } 
       , isMoney: true }, 
       answerYesNo_3: { required:true } , 

       answerYesNoText_3: { required: function(element) { 
             return $('#answerYesNo_3').val() == 'yes'; 
             } 
       , isMoney: true }, 
       answerYesNo_4: { required:true } , 

       answerYesNoText_4: { required: function(element) { 
             return $('#answerYesNo_4').val() == 'yes'; 
             } 
       , isMoney: true }, 
       answerYesNo_5: { required:true } , 

       answerYesNoText_5: { required: function(element) { 
             return $('#answerYesNo_5').val() == 'yes'; 
             } 
       , isMoney: true }  
    }, 
... 


    jQuery.validator.addMethod("isMoney", function(value, element) { 
    alert(/^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value));           
    return /^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value); 

}); 

我已經是它忽略了所需要的功能的第一部分的問題:

return $('#answerYesNo_1').val() == 'yes' 

說:我選擇「不」 answerYesNo_2,回答YesNo_3,回答YesNo_4,回答YesNo_5,我會爲他們中的每一個獲得假。如果答案是否定的,則不應該進行isMoney驗證。任何幫助?

+0

http://jsfiddle.net/k45w9/ – CFNinja

回答

0

試試這個:

$.validator.addMethod("isMoney", function (value, element) { 
    alert(/^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value)); 
    return /^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$/.test(value); 
}); 
$.validator.addMethod(
    'equalToParam', 
    function (value, element, param) { 
     return $.trim(value) == param; 
    }, 
    'Do not match.' 
); 
$('#qForm').validate({ 
    rules: { 
     answerYesNo_1: { 
      required: true, 
      equalToParam: 'yes' 
     }, 
     answerYesNoText_1: { 
      required: true, 
      isMoney: true 
     }, 
     answerYesNo_2: { 
      required: true 
     }, 
     answerYesNoText_2: { 
      required: true, 
      equalToParam: 'yes' 
      isMoney: true 
     }, 
     answerYesNo_3: { 
      required: true 
     }, 
     answerYesNoText_3: { 
      required: true, 
      equalToParam: 'yes' 
      isMoney: true 
     }, 
     answerYesNo_4: { 
      required: true 
     }, 
     answerYesNoText_4: { 
      required: true, 
      equalToParam: 'yes' 
      isMoney: true 
     }, 
     answerYesNo_5: { 
      required: true 
     }, 
     answerYesNoText_5: { 
      required: true, 
      equalToParam: 'yes' 
      isMoney: true 
     } 
    }, 

在接下來的時間,或者如果我的一段代碼不行就http://jsfiddle.net/

+0

這不演示工作。 jsFiddle是在評論部分的原始文章後面添加的。謝謝 – CFNinja