更新时间:2022-01-10 22:52:11
使用工具试用你的正则表达式比如 http://jsregex.com/ (有很多)或更好的单元测试。
Try your regex with a tool like http://jsregex.com/ (There is many) or better, a unit test.
对于天真的验证:
For a naive validation:
function validateDate(testdate) {
var date_regex = /^\d{2}\/\d{2}\/\d{4}$/ ;
return date_regex.test(testdate);
}
在您的情况下,要验证(MM / DD / YYYY),在1900年到2099年之间,我会这样写:
In your case, to validate (MM/DD/YYYY), with a year between 1900 and 2099, I'll write it like that:
function validateDate(testdate) {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(testdate);
}