简体   繁体   English

使用moment.js进行日期验证不会在给出无效日期时产生错误

[英]Date validation using moment.js is not giving error on giving invalid date

I am doing validation using angular js. 我正在使用angular js进行验证。 For date validation, I am using the moment.js file. 为了进行日期验证,我使用了moment.js文件。 My issue is when I am entering the date which is having a year as '0000' eg 1-1-0000, it is not giving the error. 我的问题是,当我输入的年份为“ 0000”(例如1-1-0000)时,没有给出错误。 My directive code is below- 我的指令代码如下-

app.directive("validDate", function () {
  return {
    require: "ngModel",
    restrict: "A", // Attributes only
    link: function (scope, elem, attr, ctrl) {
        ctrl.$validators.cValidDate = function(value) {
            if (value === undefined || value === null || value === "") {
                return true;
            }

            return moment(value, ["D-M-YYYY"], true).isValid();
        }
    }
}
});

Can anybody help me in my validation? 有人可以帮助我进行验证吗? I have created a plunker here- PLUNKER 我在这里创建了一个punker- PLUNKER

I have edited the script, But still it is now giving error for all dates less than 1-1-1970, because it is taking date 1-1-0001 as 1-1-1970 so it is validation according to it, also when no value is there in date, date is shown as invalid untill and unless I enter the date greater than 1-1-1970. 我已经编辑了脚本,但是仍然会给所有小于1-1-1970的日期给出错误,因为它将日期1-1-0001当作1-1-1970,因此根据它进行验证,当日期中没有值,除非我输入的日期大于1-1-1970,否则日期显示为无效直到。 I wanted that if year is 0000 date should not be valid. 我想如果年份是0000,则日期无效。 Can anybody help me? 有谁能够帮助我? I have updated my plunker with the above code. 我已经用上面的代码更新了我的朋克。

Update- 更新-

Now I am able to resolve the date issue but my problem is now that- if date is empty still the error is there I don't want this error when date is empty, what condition should I put for this? 现在我可以解决日期问题,但是我的问题是,如果日期仍然为空,则错误仍然存​​在,我不希望在日期为空时出现此错误,我应该为此设置什么条件? Please see my updated plunker. 请查看我更新的插件。

Answer- 回答-

I am able to made my answer, as I added the below condition - 我可以做出回答,因为我添加了以下条件-

    if(moment(value, ["D-M-YYYY"], true).isValid()){ 
               // condition
} else 
    return true;

see my new plunker here- https://plnkr.co/edit/uKcynQktBCKRYoN7xdM1?p=preview 在这里看到我的新朋克-https://plnkr.co/edit/uKcynQktBCKRYoN7xdM1 ? p = preview

Native JS Date object thinks there is a year zero as well. 本机JS Date对象认为年份也为零。 Year zero is acceptable per ISO 8601 , which uses astronomical year numbering. 根据使用天文年份编号的ISO 8601 ,零年份是可接受的。 In this system, year 0 = 1 BCE, year -1 = 2 BCE, and so forth. 在此系统中,年份0 = 1 BCE,年份-1 = 2 BCE,依此类推。

This is not true for many databases. 对于许多数据库而言并非如此。 So better set a date must be greater than validation in the subject you need. 因此,更好地设置日期必须比验证所需的日期更大。

if(moment(value, ["D-M-YYYY"], true).isValid())
            {

              //alert(moment(value, ["D-M-YYYY"], true).isSameOrAfter(moment("1-1-0001"), ["D-M-YYYY"], true));
              if(moment(value, ["D-M-YYYY"]).isSameOrAfter(moment("1-1-0000"), ["D-M-YYYY"]) )
                return true ;
              else
                return false;
            }else
            {
              return false;
            }

you might want to add a function that checks for the value of the year alone. 您可能想添加一个仅检查年份值的函数。 My solution is not elegant but it does the trick. 我的解决方案不是很好,但是可以解决问题。

function checkYear (v) {
  v = v.split("-");
  if(v[2] === "0000"){
    return true;
  }
  /* or you could go for a year of your choice*/
  /*
  if(parseInt(v[2]) <= 1900){
    return true;
  }
  */
}

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

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