gpt4 book ai didi

jQuery 验证规则 addMethod 中的信用卡到期日限制 future 20 年

转载 作者:行者123 更新时间:2023-11-30 23:59:15 24 4
gpt4 key购买 nike

嗨,我在 jquery 验证规则 validator.addMethod 上有一些代码,允许输入检查卡到期日期的 future 日期,即 10/2015 有效,我想问是否有一种方法可以添加上限到 future 20 年,因为目前他们可以输入 10/2099 或更长的卡到期日期。提前致谢!

这是我的代码,它还需要自定义消息。超出日期范围

<input type="text" name="field_4"  id="field_4" value="">



$.validator.addMethod(
"Future",
function (value, element) {
var today = new Date();
var startDate = new Date(today.getFullYear(),today.getMonth(),1,0,0,0,0);
var expDate = value;
var separatorIndex = expDate.indexOf('/');
expDate = expDate.substr( 0, separatorIndex ) + '/1' + expDate.substr( separatorIndex );
return Date.parse(startDate) <= Date.parse(expDate);
},
"Must be a valid Expiry Date"
);

$(function() {

$( "#Form" ).validate({
rules: {
field_4: {
required: true,
Future: true,
minlength: 7
},

最佳答案

一种方法:

function (value, element) {
var today = new Date();
var thisYear = today.getFullYear();
var expMonth = +value.substr(0, 2);
var expYear = +value.substr(3, 4);

return (expMonth >= 1
&& expMonth <= 12
&& (expYear >= thisYear && expYear < thisYear + 20)
&& (expYear == thisYear ? expMonth >= (today.getMonth() + 1) : true))
}

关于jQuery 验证规则 addMethod 中的信用卡到期日限制 future 20 年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26823718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com