Javascript 如何限制用户在输入字段中输入 24 格式时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11803946/
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
How to restrict user input of 24 format time into input field?
提问by iyel
I have an input field with time in HH:MM. And I need to manage that users can only enter correct time, to prevent them from entering wrong time.
我在 HH:MM 中有一个带有时间的输入字段。并且我需要管理用户只能输入正确的时间,以防止他们输入错误的时间。
A time in 24-Hour Format Regular Expression Pattern is
24 小时制正则表达式模式的时间是
([01]?[0-9]|2[0-3]):[0-5][0-9]
And I've found mask input plugin https://github.com/RobinHerbots/jquery.inputmask
我找到了掩码输入插件https://github.com/RobinHerbots/jquery.inputmask
But I dont know how to do what I want. Thx a lot.
但我不知道如何做我想做的事。多谢。
回答by Julien Ch.
回答by invertedSpear
Not sure about the plugin you referenced since I don't see a demo page for it. I find that if you need the user to enter times or dates in a certain way, you can't let them key anything. So we use calendar date pickers for dates, and this is the best time picker I've found: http://haineault.com/media/jquery/ui-timepickr/page/
不确定您引用的插件,因为我没有看到它的演示页面。我发现如果你需要用户以某种方式输入时间或日期,你不能让他们输入任何东西。所以我们使用日历日期选择器作为日期,这是我发现的最好的时间选择器:http: //haineault.com/media/jquery/ui-timepickr/page/
回答by Smith
$(selector).inputmask("h:s",{ autoUnmask:true });
回答by abasan
also can use this code
也可以使用此代码
var selector = document.getElementById("aaa");
Inputmask("(09|19|20|21|22|23):(09|19|29|39|49|59)").mask(selector);