用于验证日期格式(例如mm/yyyy)的正则表达式是什么? [英] What would be regular Expression for validating Date format like mm/yyyy.?
问题描述
用于验证日期格式(例如mm/yyyy)的正则表达式是什么.我是正则表达式的新手
What would be regular Expression for validating Date format like mm/yyyy. I am new to regular expressions
$.validator.addMethod(
"customDate",
function(value, element) {
return value.match(?);
},
"Please enter a date in the format mm/yyyy"
);
有人可以在这方面帮助我吗?
Can anyone help me in this regard?
推荐答案
扩展RobG的功能:
function validateDate(s) {
return /^([1-9]|1[0-2])\/[12]\d{3}$/.test(s);
}
([1-9] | 1 [0-2])
检查它是 1
- 9
还是 10
- 12
. [12] \ d {3}
确保年份是 1
或 2
,后跟三个数字(因此,有效范围应为 1000
- 2999
;如果您需要更具体的值,请更新您的问题.
([1-9]|1[0-2])
checks it is either 1
-9
or 10
-12
.
[12]\d{3}
makes sure the year is 1
or 2
followed by three numbers (so valid ranges in this case would be 1000
-2999
; if you need more specific values, please update your question.
编辑如果您希望月份值改为01-12,则将正则表达式更改为:
EDIT If you want month values of 01-12 instead, change the regex to:
/^(0 [1-9] | 1 [0-2])\/[12] \ d {3} $/
这篇关于用于验证日期格式(例如mm/yyyy)的正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!