简体   繁体   English

日期年份验证失败

[英]Year of date validation is failing

I need to do a date validation to accept it in dd/mm/yyyy format. 我需要进行日期验证才能以dd / mm / yyyy格式接受它。 However all conditions are working fine except that if I enter year of 6 digits it is also accepting it, like - 但是,所有条件都可以正常工作,除了如果我输入6位数字的年份也可以接受,例如-

12/12/200000 12/12/200000

as per my code is valid. 根据我的代码是有效的。 Below is my code: 下面是我的代码:

function validate(value) {
            if(!value.match(/\d\d\/\d\d\/\d\d\d\d/))
                return false;
            return checkdate(value);
}

function checkdate(val)
{
    var dates = val.split(/\D/);
    if(dates[0] <= 0 || dates[0] > 31)
        return false;
    if(dates[1] <= 0 || dates[1] > 12)
        return false;
    var now = new Date(dates[2],dates[1]-1,dates[0]);
    if (isNaN(now))
        return false;
    now.setHours(0,0,0,0);
    if (now.getFullYear() == dates[2] && now.getMonth() + 1 == dates[1] && now.getDate() == dates[0])
        return true;
    return false;
}

I am not sure why it allowing year as 6 digits valid input? 我不确定为什么允许年份为6位数有效输入?

The problem is in validate function, regular expression it matches against allows input values you don't want to pass as valid. 问题出在validate函数中,与之匹配的正则表达式允许输入您不想传递为有效值的输入值。 Besides obvious dd/mm/yyyy format, it allows found text to be anywhere in string. 除了明显的dd/mm/yyyy格式外,它还允许找到的文本位于字符串中的任何位置。 Basically, you said for it to check "if there's said expression inside string", when it should have been "if the whole string matches this expression". 基本上,您说过要检查“字符串中是否有表达式”,而应该检查“整个字符串是否与该表达式匹配”。

To fix the issue, add ^ at the beginning and $ at the end. 要解决此问题,请在开头添加^ ,在末尾添加$ ^ stands for string start and $ for string end: ^代表字符串开始, $代表字符串结束:

/^\\d\\d\\/\\d\\d\\/\\d\\d\\d\\d$/

I think you would benefit from reading documentation on regular expression syntax used by JavaScript. 我认为您将从阅读有关JavaScript使用的正则表达式语法的文档中受益。

While at at, humans tend to have issues reading long repeating sequences of similar characters, like in your regexp. 在at时,人们往往会在读取相似字符的长重复序列时遇到问题,例如在regexp中。 This expression is easer to understand and does exactly the same thing: 此表达式更易于理解,并且做的完全相同:

/^\\d{2}\\/\\d{2}\\/\\d{4}$/

You're not limiting the regex with start and stop delimiters, so 12/12/200000 is a match as it matched the regex, and then some 您并没有使用开始和停止定界符来限制正则表达式,因此12/12/200000是匹配项,因为它与正则表达式匹配,然后再匹配

if (!value.match(/^\d\d\/\d\d\/\d\d\d\d$/) )

As a sidenote, you don't have to type \\d four times, you can do \\d{4} to match four instances of \\d 附带说明,您不必键入\\d四次,可以执行\\d{4}来匹配\\d四个实例

If you want to validate a date string by creating a Date object, you don't need to check the entire pattern, just create and Date and check the result. 如果要通过创建Date对象来验证日期字符串,则无需检查整个模式,只需创建和Date并检查结果即可。 Do you really need two digits for day and month number? 您真的需要两位数字表示日期和月份吗?

If you want a 4 digit year, that must be checked separately as the constructor will happily convert two digit years to 20th century. 如果要使用4位数字的年份,则必须单独检查,因为构造函数会很乐意将两位数字的年份转换为20世纪。 If you really need two digit day and month, that can be checked at the same time as the year: 如果您确实需要两位数的日期和月份,则可以与年份同时检查:

function validateDMY(s) {
  var b = s.split(/\D/);
  var d = new Date(b[2], --b[1], b[0]);
  return d && /^\d{4}$/.test(b[2]) && b[1] == d.getMonth();
}

console.log(validateDMY('30/02/2015')); // false
console.log(validateDMY('30/22/2015')); // false
console.log(validateDMY('02/02/15'));   // false
console.log(validateDMY('30/01/2015')); // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM