var dateObj = {
'01/01/2017' : true,
'1/1/2016' : true,
'1/1/16' : true,
'1/1/116' : false,
'01/11/2016' : true,
'01.01.2016' : true,
'01.01_2016' : false,
'01-0-2016' : false,
'01-01-2016' : true
};
var failedAttempts = [];
var date_val = "01/01/2017";
var re = /^(\d{1,2})[/.\-](\d{1,2})[/.\-](\d{4}|\d{2})$/;
for(let i in dateObj){
let result = re.test(i);
if(result != dateObj[i]){
failedAttempts.push(i);
}
}
if(failedAttempts.length > 0){
console.error('Unit test fails');
console.log(failedAttempts);
}else{
console.log('Unit test pass');
}
'01-0-2016' : false
consider this case returns true even it is in wrong format. I want to rewrite the Regex with the digit matches either 1
or 2
but not 0
in single
digit.
'01 -0-2016':false认为这种情况即使格式错误也会返回true。我想重写正则表达式,数字匹配为1或2,但不是0,单位数。
4 个解决方案
#1
2
Restrict the digit matching patterns with negative (?!0+\b)
lookaheads:
使用负(?!0+ \ b)前瞻限制数字匹配模式:
/^(?!0+\b)(\d{1,2})[\/.-](?!0+\b)(\d{1,2})[\/.-](\d{4}|\d{2})$/
^^^^^^^^ ^^^^^^^^
See the regex demo
请参阅正则表达式演示
If you needn't restrict both day month parts, just remove the unnecessary lookahead.
如果您不需要限制两个月的零件,只需删除不必要的前瞻。
The (?!0+\b)
pattern matches one or more zeros followed with a word boundary (that is, there cannot be any letter/digit/_
after it), and if the pattern is found, the match is failed.
(?!0+ \ b)模式匹配一个或多个零后跟一个单词边界(即,后面不能有任何字母/数字/ _),如果找到模式,则匹配失败。
#2
0
There could be several ways, I propose:
我建议可以有几种方法:
var re = /^(\d{1,2})[/.\-]([1-9]|[0-9]{2})[/.\-](\d{4}|\d{2})$/;
Let me know if it solves your issue.
如果它能解决您的问题,请告诉我。
#3
0
Here is a non-regex way of validating your input date strings using dd/mm/yyyy
or /dd/mm/yy
formats:
以下是使用dd / mm / yyyy或/ dd / mm / yy格式验证输入日期字符串的非正则表达方式:
for(let str in dateObj) {
var parts = str.split(/[-.\/]/);
var p = parts[0]*parts[1]*parts[2];
if (isNaN(p) || p<10 || (p>99 && p<1000)) {
console.log("Invalid date: ", str);
}
}
Output:
Invalid date: 1/1/116
Invalid date: 01.01_2016
Invalid date: 01-0-2016
- First we are splitting date string into 3 parts using delimiters
-
or.
or/
- Then we are multiplying all 3 parts and taking the product for validation
- Finally we are using
isNaN(p) || p<10 || (p>99 && p<1000
conditions for validation.isNan
will address all invalid date inputs,p<=0
will address day, mont or year being zero and last condition will address year part in 3 digits.
首先,我们使用分隔符将日期字符串拆分为3个部分 - 或者。要么 /
然后我们将所有3个部分相乘并将产品用于验证
最后我们使用isNaN(p)|| p <10 || (p> 99 && p <1000条件用于验证.isNan将解决所有无效日期输入,p <= 0将解决日期,mont或年份为零,最后一个条件将解决年份部分的3位数。
#4
0
This should work for you:
这应该适合你:
const validateDate = (date) => /^(?:[1-2]\d|0?[1-9]|30|31)([.\/-])(?:0?[1-9]|1[0-2])\1(?:\d{4}|\d{2})$/.test(date)
const testData = [`01/01/2017`, `1/1/2016`, `1/1/16`, `01/11/2016`, `01.01.2016`, `01-01-2016`, `01.0.2016`, `01.01_2016`, `01-0-2016`, `1/1/116`, `41.23.23`]
for (const date of testData){
console.log(`${date} ${validateDate(date)}`)
}
#1
2
Restrict the digit matching patterns with negative (?!0+\b)
lookaheads:
使用负(?!0+ \ b)前瞻限制数字匹配模式:
/^(?!0+\b)(\d{1,2})[\/.-](?!0+\b)(\d{1,2})[\/.-](\d{4}|\d{2})$/
^^^^^^^^ ^^^^^^^^
See the regex demo
请参阅正则表达式演示
If you needn't restrict both day month parts, just remove the unnecessary lookahead.
如果您不需要限制两个月的零件,只需删除不必要的前瞻。
The (?!0+\b)
pattern matches one or more zeros followed with a word boundary (that is, there cannot be any letter/digit/_
after it), and if the pattern is found, the match is failed.
(?!0+ \ b)模式匹配一个或多个零后跟一个单词边界(即,后面不能有任何字母/数字/ _),如果找到模式,则匹配失败。
#2
0
There could be several ways, I propose:
我建议可以有几种方法:
var re = /^(\d{1,2})[/.\-]([1-9]|[0-9]{2})[/.\-](\d{4}|\d{2})$/;
Let me know if it solves your issue.
如果它能解决您的问题,请告诉我。
#3
0
Here is a non-regex way of validating your input date strings using dd/mm/yyyy
or /dd/mm/yy
formats:
以下是使用dd / mm / yyyy或/ dd / mm / yy格式验证输入日期字符串的非正则表达方式:
for(let str in dateObj) {
var parts = str.split(/[-.\/]/);
var p = parts[0]*parts[1]*parts[2];
if (isNaN(p) || p<10 || (p>99 && p<1000)) {
console.log("Invalid date: ", str);
}
}
Output:
Invalid date: 1/1/116
Invalid date: 01.01_2016
Invalid date: 01-0-2016
- First we are splitting date string into 3 parts using delimiters
-
or.
or/
- Then we are multiplying all 3 parts and taking the product for validation
- Finally we are using
isNaN(p) || p<10 || (p>99 && p<1000
conditions for validation.isNan
will address all invalid date inputs,p<=0
will address day, mont or year being zero and last condition will address year part in 3 digits.
首先,我们使用分隔符将日期字符串拆分为3个部分 - 或者。要么 /
然后我们将所有3个部分相乘并将产品用于验证
最后我们使用isNaN(p)|| p <10 || (p> 99 && p <1000条件用于验证.isNan将解决所有无效日期输入,p <= 0将解决日期,mont或年份为零,最后一个条件将解决年份部分的3位数。
#4
0
This should work for you:
这应该适合你:
const validateDate = (date) => /^(?:[1-2]\d|0?[1-9]|30|31)([.\/-])(?:0?[1-9]|1[0-2])\1(?:\d{4}|\d{2})$/.test(date)
const testData = [`01/01/2017`, `1/1/2016`, `1/1/16`, `01/11/2016`, `01.01.2016`, `01-01-2016`, `01.0.2016`, `01.01_2016`, `01-0-2016`, `1/1/116`, `41.23.23`]
for (const date of testData){
console.log(`${date} ${validateDate(date)}`)
}