我们有一个使用 JQuery Validation Plugin 的表单,并希望忽略任何尚未更改其默认值的表单字段值。
例如,我们有一张信用卡更新表格,只显示最后四张信用卡号码以确保安全。它看起来像这样:
** * * 5629
每个用户的默认值都不同,因为最后四张信用卡会有所不同。
如果用户在个人资料更新期间未更改其默认信用卡值,我们希望绕过此表单字段上的验证路由,因为它显然不会验证为有效的信用卡号。
以下是我们当前的验证摘要:
card_number: {
required: true,
creditcard: true
}
messages: {
card_number: {
required: "Please enter a <strong>valid credit card number</strong>.",
}
以下是表单字段:
<input type="text" name="card_number" id="card_number" title="Your credit card number." value="************ 5629" />
我们如何忽略验证尚未更改其默认值的任何表单字段值?
感谢。
===============================================
ANDREW新注意事项:
由于我们验证信用卡类型的方式,我认为我们遇到了一些小故障。
以下是完整代码段以及初始修复:
card_number: {
creditcardtypes: function() {
// return an object with a property named the same as the pay method
var result = new Object();
var propertyName = $('select[name="pay_method"]').val().toLowerCase();
var propertyValue = 1;
eval("result."+propertyName+"='"+propertyValue+"'");
return result;
},
ccnotdefault: true,
required: true
},
我们如何才能容纳信用卡TYPE规则?目前,验证失败,因为默认信用卡值不是有效的信用卡类型。
再次感谢Andrew。
=============================================== =
FORM FIELDS:
<!--- PAYMENT METHOD FIELD --->
<tr>
<td style="padding-top:15px; text-align:right; width:45%;"><label for="pay_method"> Payment Method:
<span class="orderlineredsm"><strong> * </strong></span></label></td>
<td style="padding-top:15px; text-align:left; width:55%;" colspan="2" align="left" id="pmethod">
<select name="pay_method" id="pay_method" class="highlightme required" style="width: 115px;" onchange="jQuery('#card_number').valid()" title="Your Payment Method.">
<option value="Visa" <cfif form.pay_method EQ "Visa">selected="selected"</cfif>>Visa</option>
<option value="Mastercard" <cfif form.pay_method EQ "Mastercard">selected="selected"</cfif>>Mastercard</option>
<option value="Amex" <cfif form.pay_method EQ "Amex">selected="selected"</cfif>>Amex</option>
<option value="Discover" <cfif form.pay_method EQ "Discover">selected="selected"</cfif>>Discover</option>
<option value="Diners" <cfif form.pay_method EQ "Diners">selected="selected"</cfif>>Diners</option>
</select>
</td>
</tr>
<!--- END PAYMENT METHOD FIELD --->
<!--- CARD NUMBER FIELD --->
<tr>
<td style="text-align:right; width:45%;"><label for="card_number"> Credit Card Number:
<span class="orderlineredsm"><strong> * </strong></span></label></td>
<td style="text-align:left; width:55%;" colspan="2" align="left" id="cnumber">
<input type="text" name="card_number" id="card_number" class="highlightme" style="width: 130px;" maxlength="19" title="Your credit card number." value="************<cfoutput>#form.card_number#</cfoutput>" onfocus="clearText(this)" onblur="clearText(this)" />
<span class="create-tooltip" title="For your protection and security, only the <strong>last four digits of your credit card number</strong> are shown.<br /><br /><strong>NOTE:</strong> Only change if you are updating your credit card number."><img src="/public/images/help_25.png" style="margin-bottom:1px;" alt="help" width="25" height="25" /></span>
</td>
</tr>
<!--- CARD NUMBER FIELD --->
=============================================== ===========
可能修正
card_number: {
ccnotdefault: function(){ return $('#pay_method').val(); },
required: true
},
jQuery.validator.addMethod("ccnotdefault", function(value, element, param) {
// if the element is optional, don't validate
return this.optional(element) ||
// or if the value is equal to the default value
(value === element.defaultValue ||
// or, finally, if the cc number is valid
jQuery.validator.methods.creditcard2.call(this, value, element, param));
}, jQuery.validator.messages.creditcard);
http://www.ihwy.com/labs/downloads/jquery-validate-creditcard-2/jquery.validate.creditcard2-1.0.1.js
答案 0 :(得分:2)
我会为此编写一个自定义规则来调用默认信用卡规则,但也会考虑该字段的默认值:
$.validator.addMethod("ccnotdefault", function(value, element) {
// if the element is optional, don't validate
return this.optional(element) ||
// or if the value is equal to the default value
(value === element.defaultValue ||
// or, finally, if the cc number is valid
$.validator.methods.creditcard.call(this, value, element));
}, $.validator.messages.creditcard);
$(document).ready(function() {
$("#myform").validate({
rules: {
card_number: {
ccnotdefault: true,
required: true
}
}
});
});