2010-06-16 70 views
1

我一直希望有人能指出我正确的方向。我正在寻找在我的表单中使用this plugin以及jquery.validation插件,但是从我所看到的我需要使用选择标记才能使creditcard2正常工作。如何使用jquery.validation creditcard2验证信用卡?

我现在有是这样的:

<p class="ccType"> 
    <label for="card_type" class="error">You must select a credit card from one of the following.</label> 
     <br> 
    <input type="radio" value="1000" id="card_type_1000" name="card_type" style="outline:none; border:none;" /> 
     <img src="../visa.png" alt="Visa" width="50" height="30" align="top" /> 
     <input type="radio" value="1002" id="card_type_1002" name="card_type" style="outline:none; border:none;" /> 
     <img src="../mastercard.png" alt="Mastercard" width="50" height="30" align="top" /> 
     <input type="radio" value="1006" id="card_type_1006" name="card_type" style="outline:none; border:none;" /> 
     <img src="../discover.png" alt="Discover" width="50" height="30" align="top" /> 
     <input type="radio" value="1004" id="card_type_1004" name="card_type" style="outline:none; border:none;" /> 
     <img src="../amex.png" alt="American Express" width="50" height="30" align="top" /> 
    </p> 

值= 1000是 “签证”

值= 1002是 “万事达卡”

值= 1006是 “发现”

值= 1004是“美国运通”

插件站点提到创建查找哈希值。我不知道如何创建一个。此外,网站上的文档仅显示使用<select></select>的示例。我正在使用输入。

我如何能得到这个与设置,我现在有工作。

有什么建议吗?

回答

1

的查找哈希看起来像

var hash = { 
    1000: "Visa", 
    1002: "MasterCard", 
    1004: "AmEx", 
    1006: "Discover" 
}; 

那么你可以使用下面的验证功能,当然内置到您的当前设置的:

$("#myform").validate({ 
    rules: { 
     cardnum: { // this should be the id of the card number field 
      creditcard2: function(){ return hash[$('.ccType > input:checked').val()]; } 
     } 
    }  
}); 

这个相当简洁的代码首先查找所有inputs在您的.ccType元素中。然后它选择一个被检查的(如果有的话)并获得它的值。然后将值通过散列推入,以将其更改为插件所喜欢的值。而且...让我们希望这个工程。

+0

是的......让我试试这个并回复你。谢谢! – TikaL13 2010-06-16 21:08:44

+0

所以...它的工作? – MvanGeest 2010-06-16 21:34:59

+0

是的,先生!...谢谢! – TikaL13 2010-06-16 21:35:21