开始日期和结束日期之间的 jQuery-Validation-Engine 验证

标签 jquery jquery-validation-engine

我使用 jQuery 验证引擎进行表单验证,这里是演示链接:

https://github.com/posabsolute/jQuery-Validation-Engine

我有两个文本框,一个用于开始日期,第二个用于结束日期。我想验证一下 开始日期不得大于结束日期,并且结束日期不得小于开始日期 >。

如果有任何验证,请告诉我。

最佳答案

将值转换为 Date 对象。然后你可以用它来比较,例如。转换为 Unix 时间戳或其他东西...

请参阅此处了解有关日期的更多信息:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

编辑:我将包含更多关于我的意思的信息......在 jQuery Validation Engine验证器部分下页面,查找描述 funcCall[methodName] 的部分。

funcCall[methodName] - Validates a field using a third party function call. If a validation error occurs, the function must return an error message that will automatically show in the error prompt.

因此,您可以将函数处理程序附加到该字段,该函数将获取日期,然后比较它们。

<input value="" class="validate[required,funcCall[checkDates]]" type="text" id="startdate" name="startdate" />
<input value="" class="validate[required,funcCall[checkDates]]" type="text" id="enddate" name="enddate" />

在页面的某个地方,您有 JavaScrip 函数:

var checkDates = function(field, rules, i, options) {
    // Do your date comparisons here between startdate and enddate values
};

编辑#2:

还有:

past[NOW, a date or another element's name] - Checks if the element's value (which is implicitly a date) is earlier than the given date. When "NOW" is used as a parameter, the date will be calculate in the browser. When a "#field name" is used ( The '#' is optional ), it will compare the element's value with another element's value within the same form. Note that this may be different from the server date. Dates use the ISO format YYYY-MM-DD

future[NOW, a date or another element's name] - Checks if the element's value (which is implicitly a date) is greater than the given date. When "NOW" is used as a parameter, the date will be calculate in the browser. When a "#field name" is used ( The '#' is optional ), it will compare the element's value with another element's value within the same form. Note that this may be different from the server date. Dates use the ISO format YYYY-MM-DD

如果这符合您的喜好,这是标记:

<input value="" class="validate[required,past[#enddate]]" type="text" id="startdate" name="startdate" />
<input value="" class="validate[required,future[#startdate]]" type="text" id="enddate" name="enddate" />

关于开始日期和结束日期之间的 jQuery-Validation-Engine 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15287574/

相关文章:

php - 使用 AJAX 初始化 jQuery Multiselect

JQuery 验证引擎

Jquery ValidationEngine 基于下拉选择的交换规则

jquery - 自定义验证规则 - jQuery 验证引擎

javascript - 如何显示错误消息以便 jquery 验证引擎?

javascript - 如何更改电子邮件的 html5 模式错误消息

javascript - 悬停结束时二级菜单消失

javascript - 导航 CSS 样式不起作用

javascript - 鼠标滚轮水平滚动

jQuery 验证禁用提交按钮,直到表单验证