jQuery 将信用卡到期日期验证为未来日期

发布于 2024-09-08 17:24:58 字数 387 浏览 1 评论 0原文

我似乎无法做到这一点,我可以让它捕获过去的日期,但不能在未来的日期返回 true。我只需要验证我的表单的信用卡到期日期是在未来,这是行不通的,有什么想法吗?日期必须采用 MM/YYYY 格式,中间有“/”。

$.validator.addMethod(
"FutureDate",
function(value, element) {
    var startdatevalue = (.getMonth/.getYear);
    return Date.parse(startdatevalue) < Date.parse($("#ExpirationDate").val());
}, 
    "End Date should be greater than Start Date."
);

I can't seem to get this right, I can get it to catch a past date, but not return true on future date.I just need to validate my form's Credit Card Expiration Date as being in the future, this isn't working, any ideas? the date has to be in the format MM/YYYY with a "/" in between them.

$.validator.addMethod(
"FutureDate",
function(value, element) {
    var startdatevalue = (.getMonth/.getYear);
    return Date.parse(startdatevalue) < Date.parse($("#ExpirationDate").val());
}, 
    "End Date should be greater than Start Date."
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-09-15 17:24:58

你实际上并没有让它赶上过去的约会。如果您将像“11/2010”这样的日期传递给 Date.parse(),它将返回 NaN(或不是数字),这是逻辑相当于返回false

尝试这样做看看我的意思:

alert( Date.parse('11/2010') );

如果你添加一个日期数字,它应该可以工作。类似于:

var startdatevalue = '11/1/2010';

当然,这个示例使用硬编码日期。如果值存储为 11/2010,您可以尝试如下操作:

    // Get the index of the "/"
var separatorIndex = value.indexOf('/');

    // Add "/1" before the separatorIndex so we end up with MM/1/YYYY
var startDate = value.substr( 0, separatorIndex ) + '/1' + value.substr( separatorIndex );

    // Do the same with the expiration date
var expDate = $("#ExpirationDate").val();
separatorIndex = expDate.indexOf('/');
expDate = expDate.substr( 0, separatorIndex ) + '/1' + expDate.substr( separatorIndex );

    // Return the comparison
return Date.parse(startDate) < Date.parse(expDate);

You're not actually getting it to catch a past date. If you're passing a date like this "11/2010" to your Date.parse(), it is returning NaN (or Not a Number) which is the logical equivalent to returning false.

Try doing this to see what I mean:

alert( Date.parse('11/2010') );

If you add a day number, it should work. Something like:

var startdatevalue = '11/1/2010';

Of course, this example uses a hard coded date. If the values are stored as 11/2010, you could try something like this:

    // Get the index of the "/"
var separatorIndex = value.indexOf('/');

    // Add "/1" before the separatorIndex so we end up with MM/1/YYYY
var startDate = value.substr( 0, separatorIndex ) + '/1' + value.substr( separatorIndex );

    // Do the same with the expiration date
var expDate = $("#ExpirationDate").val();
separatorIndex = expDate.indexOf('/');
expDate = expDate.substr( 0, separatorIndex ) + '/1' + expDate.substr( separatorIndex );

    // Return the comparison
return Date.parse(startDate) < Date.parse(expDate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文