0

我继承了以下脚本,需要对其进行改进。该函数检查是否只输入了允许的字符 (0123456789/),然后将输入的日期格式化为 1/1/12 将重新格式化为 01/01/2012。稍作调整后,这部分工作正常。我现在需要进一步验证,如果省略了年份,则添加年份,这意味着如果用户输入 1/1,则需要对其进行格式化并添加当前年份(例如 01/01/2012)。

用户输入和所需(工作)输出的示例

  • a/a/a 警告用户错误 - 检查
  • 1/2/10 将输入字段更新为 01/03/2010
  • 01/01/12 将输入字段更新为 01/01/2012
  • 1/10/2 将输入字段更新为 01/10/2002

所需更新(除上述内容外)1/9 更新输入字段为 01/09/2012

这是当前功能(只要保留上述功能,欢迎您更改,重写,等等)。jQuery 1.7 库正在使用中,可以实现。

function ValidateDate(obj)
{
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator has to be /
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy

PARAMETERS:
   ValidateDate(strValue) - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
    var checkOK = "0123456789/";
    var checkStr = obj.value;
    var allValid = true;
    var p = /(\d{1,2})\/(\d{1,2})\/(\d{1,4})/;
    var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
    // check to see if valid characters were used       
    for (i = 0;  i < checkStr.length;  i++)
    {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
          if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
    }
    if (!allValid)
    {
        alert("Please use only a combination of " + checkOK + "\'s charaters in the date field.  Dates should be entered in the format of mm/dd/yyyy.");
        setTimeout((function() { obj.select() }), 0);
        return (false);
    }       
    //  converts to mm/dd/yyyy format
    if (!obj.value.match(p)) return;
    num=new Array();
    num=obj.value.match(p);
    if (num[1].length == 1) num[1]="0" + num[1];
    if (num[2].length == 1) num[2]="0" + num[2];
    if (num[3].length == 1) num[3]="200" + num[3];
    if (num[3].length == 2) num[3]="20" + num[3];
    obj.value= num[1] + "/" + num[2] + "/" + num[3];
    //check to see if in correct format
    if(!objRegExp.test(obj.value))
    {
        alert('The date entered is not properly formatted.');
        return false; //doesn't match pattern, bad date
    }
    else{
        var arrayDate = obj.value.split(RegExp.$1); //split date into month, day, year
        var intDay = parseInt(arrayDate[1],10); 
        var intYear = parseInt(arrayDate[2],10);
        var intMonth = parseInt(arrayDate[0],10);
    //check for valid month
    if(intMonth > 12 || intMonth < 1) {
        alert('The date entered is invalid');
        return false;
    }

    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }

    //check for February
    var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
        return true; //Feb. had valid number of days
    }
    alert(obj.value + ' is not a valid date.');
    //  return false; //any other values, bad date
}
4

1 回答 1

0

反对的几点:

  • 您不必检查有效字符,因为您的匹配正则表达式已经需要它们。如果你真的想,使用/^[\d\/]*$/.test()而不是那个循环。
  • 要匹配 1/1 之类的日期,请使用/\d{1,2}\/\d{1,2}(\/\d{1,4})?/asp并仅执行 anum = obj.value.split("/")而不是匹配组
  • 要验证日期,请查看使用日期对象的 javascript 日期验证
  • 您还应该允许 ISO 日期格式 YYYY-MM-DD,它由Date()
于 2012-04-09T19:42:48.510 回答