本文介绍了jquery验证将来的日期addMethod();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jquery validate plugin的一些问题,以及将来的日期。

Some question for jquery validate plugin and only date in future.

我有一个像这样的输入字段:

I've got an input field like this:

<input id="date_hinfahrt" class="gui-input error" type="date" required="" name="datefuture" placeholder=" DD/MM/YYYY" aria-required="true" aria-invalid="true">

我不想要日期选择器!只有这样的面具:

 $("#datefuture").mask('99/99/9999', {placeholder:'_'});

对于validate插件,我试试这个addMethod();
但它没有用,我希望有人可以帮助我。

And for the validate plugin I've try this addMethod();but it's not working and I hope someone can help me.

$.validator.addMethod("minDate", function(value, element) {
    var now = new Date();
    var myDate = new Date(value);
    var mD = ("0" + (myDate.getDate())).slice(-2) + '/' + ("0" + (myDate.getMonth()+1)).slice(-2) + '/' + myDate.getFullYear();
    var nowdate = ("0" + (now.getDate())).slice(-2) + '/' + ("0" + (now.getMonth()+1)).slice(-2) + '/' + now.getFullYear();
    return this.optional(element) || mD > nowdate;
    // else alert('Das passt nicht!' + mD +  '   ' + nowdate);
});


推荐答案

你可以直接比较日期,你不能需要做所有连接。

You can just compare dates directly, you don't need to do all that concatenation.

jQuery.validator.addMethod("minDate", function (value, element) {
    var now = new Date();
    var myDate = new Date(value);
    return this.optional(element) || myDate > now;

    // else alert('Das passt nicht!' + mD +  '   ' + nowdate);

});

您设置小提琴的方式也存在问题。您有 name =date_hinfahrt,但在规则:选项中,您使用了 datefuture 。此外, formtools.js 不能与 type =date输入一起使用,因此它正在清空该字段,导致它总是抱怨该领域是必需的;我将其更改为 type =text

There were also problems in the way you set up your fiddle. You had name="date_hinfahrt", but in your rules: option you used datefuture. Also, formtools.js doesn't work with type="date" inputs, so it was emptying the field, causing it always to complain that the field is required; I changed it to type="text".

这篇关于jquery验证将来的日期addMethod();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:49