Home  >  Article  >  Web Front-end  >  JS verification date format YYYY-mm-dd specific implementation_javascript skills

JS verification date format YYYY-mm-dd specific implementation_javascript skills

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

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