特定日期格式的正则表达式 [英] regex for a particular date format
问题描述
我想要一个用于日期格式的正则表达式,例如: 2011年1月1日
I want to have a regex for a date format, Example: 01-Jan-2011
我写了^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}
,但没有检查所有有效日期,例如50-AAA-2011也将被视为有效.
I have written ^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}
but does not check for all the valid dates like 50-AAA-2011 will also be considered as valid.
我正在使用JavaScript中的实时验证 lib
I am using Live Validation lib in javascript
我的代码
var sayDate = new LiveValidation('date');
sayDate.add( Validate.Format, { pattern: /^[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}/i });
请帮助!
推荐答案
尝试使用正则表达式验证日期格式实际上比听起来复杂得多.您可以很轻松地进行基本验证,但是要使其完美,总是有很多工作要做.
Trying to validate a date format using regex is actually a lot more complex than it sounds. You can do basic validation quite easily, but there's always that bit more to do to get it perfect.
我建议使用日期选择器控件代替它,它可以为您进行验证(并且还为您提供漂亮的闪亮用户界面功能),或者可以使用现有的Javascript库之一来处理日期你.
I would suggest instead using either a date picker control, which can do the validation for you (and also gives you the bonus of a nice shiny user interface feature), or else one of the existing Javascript libraries which can handle dates for you.
对于日期选择器控件,有任意数量的jQuery选项.对于日期处理库,我建议查找 date.js .
For date picker controls, there are any number of jQuery options. For date handling libraries, I would suggest looking up date.js.
希望有帮助.
这篇关于特定日期格式的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!