Jquery - 验证表单上的时间值 - 正确的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12029111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - Validating time value on a form - correct way?
提问by Damien-Data
After google searching, searching this site as well as jQueryfound there was no easy way to validate time value.
在谷歌搜索后,搜索这个站点以及jQuery发现没有简单的方法来验证时间值。
By the looks of things it suggest using addMethodand use the to regexpess the value to check against what time should look like.
从外观上看,它建议使用addMethod并使用 regexpess 值来检查时间应该是什么样子。
However decided to look at the jquery.validate.js
and how dateISO was checked, it looked lot easier way to do it, so I then copied all data relating to dateISO and called it timeISO and changed the string to validate time and used the following time sting
然而决定查看jquery.validate.js
dateISO 以及如何检查它,它看起来更容易做到这一点,所以我然后复制了与 dateISO 相关的所有数据并将其称为 timeISO 并更改字符串以验证时间并使用以下时间字符串
^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$
Is there some reason why one should not modify jquery.validate.js
and do it the way I have? Just a little bit worried as all my searching on google and this forum it was never suggested.
有什么理由不应该修改jquery.validate.js
并按照我的方式进行操作吗?只是有点担心,因为我在谷歌和这个论坛上的所有搜索都没有被建议。
回答by Fabrício Matté
You can add custom methods to jQuery.validate
. There's probably one available for this but I coded this 24h time format validation as an example:
您可以将自定义方法添加到jQuery.validate
. 可能有一个可用于此,但我将此 24 小时时间格式验证编码为示例:
$.validator.addMethod("time24", function(value, element) {
if (!/^\d{2}:\d{2}:\d{2}$/.test(value)) return false;
var parts = value.split(':');
if (parts[0] > 23 || parts[1] > 59 || parts[2] > 59) return false;
return true;
}, "Invalid time format.");
文档。
You can also sum it up in a single regular expression (source):
您还可以用一个正则表达式 ( source) 对其进行总结:
([01]?[0-9]|2[0-3])(:[0-5][0-9]){2}
And if you're using HTML5, you can use the pattern
attribute:
如果您使用的是 HTML5,则可以使用该pattern
属性:
<input type="text" pattern="([01]?[0-9]|2[0-3])(:[0-5][0-9]){2}"
required="required" placeholder="hh:mm:ss" />
Keep using the jQuery.validate
method if you need compatibility with older browsers though:
jQuery.validate
如果您需要与旧浏览器兼容,请继续使用该方法:
$.validator.addMethod("time24", function(value, element) {
return /^([01]?[0-9]|2[0-3])(:[0-5][0-9]){2}$/.test(value);
}, "Invalid time format.");
回答by Silvestre
I know it's been a while, but I think the best solution is to check the additional-methods.jslibrary. (You can download it from http://jqueryvalidation.org/)
我知道已经有一段时间了,但我认为最好的解决方案是检查additional-methods.js库。(您可以从http://jqueryvalidation.org/下载)
There are two validations: "time12h"and "time".
有两个验证:"time12h"和"time"。
回答by Christofer Eliasson
Modifying the plugin core makes it more difficult to update to a new version of the plugin, without replacing your modifications, and is rarely a good idea.
修改插件核心会使更新到插件的新版本变得更加困难,而不替换您的修改,这很少是一个好主意。
I'd suggest that you place your custom code in a separate file, instead of altering the core code.
我建议您将自定义代码放在一个单独的文件中,而不是更改核心代码。