Home  >  Article  >  Web Front-end  >  JS验证日期的格式YYYY-mm-dd 具体实现_javascript技巧

JS验证日期的格式YYYY-mm-dd 具体实现_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:30:471301browse
复制代码 代码如下:

function checkInputDate(obj){
   var   strDate=obj.value;
   var  re =/^(\d{4})-(\d{2})-(\d{2})$/;
   if(re.test(strDate))//判断日期格式符合YYYY-MM-DD标准
   {
    var   dateElement=new   Date(RegExp.$1,parseInt(RegExp.$2,10)-1,RegExp.$3);
     if(!((dateElement.getFullYear()==parseInt(RegExp.$1))&&((dateElement.getMonth()+1)==parseInt(RegExp.$2,10))&&(dateElement.getDate()==parseInt(RegExp.$3))))//判断日期逻辑
     {
       document.getElementById("errorMessage").innerText = "You can only input Date.(YYYY-MM-DD) !";
       document.getElementById("error").style.display = "";
       obj.value= ' ';
      }
   }else{
    document.getElementById("errorMessage").innerText = "You can only input Date.(YYYY-MM-DD)!";
       document.getElementById("error").style.display = "";
       obj.value= '';
   }
 }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn