2013-04-02 50 views
0

是否有任何方式接受數爲10〜50與「AB」和「NF'-jQuery的陣列驗證

以下代碼接受0到任意數量的沿‘AB’和‘NF’沿

<script> 
     $.validator.addMethod("custom_number", function(value, element) { 
      return this.optional(element) || value == "AB" || value == "NF" || 
       value.match(/^[0-9,\+-]+$/); 
     }, "Please enter a valid number, or 'AB'"); 

     $(document).ready(function(){ 
      $("#form").validate({ 
       rules: { 
        'hd[]': { 
         required: false, 
         custom_number: true 
        } 
       } 
      }); 
     }); 
    </script> 

在此先感謝。

回答

0

您可以使用<>

this.optional(element) || value == "AB" || value == "NF" || 
      || (value < 50 && value > 10); 

或者在一個正則表達式:

this.optional(element) || value.match(/^[1-5][0-9]|AB|NF$/); 

[1-5][0-9]|AB|NF意味着:匹配[1-5][0-9](10 - 50)或ABNF^...$確保正則表達式只匹配比賽前後沒有其他字符(例如fooNFbar