JavaScript校验日期格式

 网页中经常会有些输入框是要求输入日期格式的,如果输入的不是日期格式就需要提示用户重新输入,以下的JavaScript支持YYYY-MM-DD或者YYYYMMDD形式的日期输入,判断规则很简单,首先判断输入的位数是否在8~10之间,然后将输入的内容中的‘-’符号替换成为‘1’,对替换后的内容做判断看是否全部为数字,如果输入的内容中包含‘-’,则判断最后一次出现‘-’和第一次出现‘-’的位置之差是否为不大于3,因为两者之间是月份,而月份有可能输入一位或两位,所以两者之间的最大差值是3,然后分别从输入的字符串中获取年月日,再判断年份是否闰年来判断每个月的最后一天,看看输入的天数是否超过了每个月的最后一天,从而来判断输入的是否为日期,该判断灵活性差,假如用户提出YYYY/MM/DD形式日期的输入就得改代码,不如正则表达式来的方便,如果有更好的办法希望大家提出,我们共同进步!

新建一个页面,页面中就有个输入框,该输入框中输入日期,在其onblur事件中调用日期格式判断方法。

<form name=fm >

      <input type=text name=StartStatDate  class=input description="统计期间开始日期" onblur="checkDateFormate(this);"/>

</form>

//判断输入的内容是否为日期格式

function checkDateFormate(Field) {

    var inputDateValue = Field.value;

    var desc = Field.description;

    if(inputDateValue == null || inputDateValue == '') {

       return false;

    }

    //获取输入字符串的长度

    var inputValueLength = inputDateValue.length;

    //如果满足下面判断的所有条件才算合法的日期,否则不合法

    if(checkNumeric(inputDateValue) && checkLegth(inputValueLength) && checkSpecialChar(inputDateValue) ) {

       return true;

    }else {

errorMessage("请输入合法的" + desc +"\n类型为日期,格式为YYYY-MM-DD 或者YYYYMMDD");

       Field.focus();

       return false;

    }

}

//日期只能是8~10

function checkLegth(length) {

    if(length < 8 || length > 10) {

       return false;

    }

    return true;

}

//如果输入的内容中包含‘-’,则按照‘-’分割来去年月日,否则直接按照位数取

function checkSpecialChar(inputDateValue) {

    var index = inputDateValue.indexOf('-');

    var year = 0;

    var month = 0;

    var day = 0;

    if(index > -1) {

       var lastIndex = inputDateValue.lastIndexOf('-');

       //只能是YYYY-M-DD或者YYYY-MM-DD的形式

       if(lastIndex - index < 1 || lastIndex - index > 3) {

           return false;

       }

       var arrDate = inputDateValue.split('-');

           year = arrDate[0];

           month = arrDate[1];

           day = arrDate[2];

       } else {

           year = inputDateValue.substring(0,4);

           month = inputDateValue.substring(4,6);

           day = inputDateValue.substring(6,8);

       }

       if(Number(month) > 12 || Number(day) > 31 ||Number(month)<1

                           || Number(day)<1 ||  year.length != 4) {

           return false;

    } else  if(day > getLastDayOfMonth(Number(year),Number(month))) {

           return false;

    }

    return true;

}

//判断输入的内容将‘-’替换成为数字1后,是否全部为数字

function checkNumeric(inputDateValue) {

    var replacedValue = inputDateValue.replace(/-/g,'1');

    return isNumeric(replacedValue);

}

//判断是否为数字

function isNumeric(strValue)

{

  var result = regExpTest(strValue,/\d*[.]?\d*/g);

  return result;

}

 

function regExpTest(source,re)

{

  var result = false;

 

  if(source==null || source=="")

    return false;

 

  if(source==re.exec(source))

    result = true;

 

  return result;

}

 

//获得一个月中的最后一天

function getLastDayOfMonth(year,month){

    var days=0;

    switch(month){

    case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31;break;

    case 4: case 6: case 9: case 11: days=30;break;

    case 2: if(isLeapYear(year)) days=29;else days=28;break;

    }

    return days;

}

//判断是否为闰年

function isLeapYear(year){

    if((year %4==0 && year %100!=0) || (year %400==0)) return true;

    else return false;

}

原文地址:https://www.cnblogs.com/gmq/p/1526216.html