假设你输入框里取过来的值为keyValue,判断如下:
if(!isValidDate(keyValue))
{
    alert("输入的日期格式必须为yyyy-mm-dd!");
    formName.inputName.focus();
    return false;
}

解决方案 »

  1.   

    javascript里没有isValidDate()函数巴
      

  2.   

    <input type="text" name="abc" onchange="vbscript:msgbox(isdate(document.abc.value))">
      

  3.   

    怎么会没有isValidDate函数,
    我刚用过的。
      

  4.   

    function isDate(DateVal) 
    {
    var inputStr = DateVal;
    // convert hyphen delimiters to slashes
    while (inputStr.indexOf("-") != -1) 
    {
    inputStr = replaceString(inputStr,"-","/");
    }
    var delim1 = inputStr.indexOf("/");
    var delim2 = inputStr.lastIndexOf("/");
    if (delim1 != -1 && delim1 == delim2)
    {
    // there is only one delimiter in the string
    alert("日期格式错误。\n正确的格式为:yyyymmdd,yyyy/mm/dd/yyyy,或yyyy-mm-dd。");
    return false;
    }
    if (delim1 != -1) 
    {
    // there are delimiters; extract component values
    var yyyy = parseInt(inputStr.substring(0,delim1),10);
    var mm = parseInt(inputStr.substring(delim1 + 1,delim2),10);
    var  dd = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);

    else 
    {
    // there are no delimiters; extract component values
    var yyyy = parseInt(inputStr.substring(0,2),10);
    var mm = parseInt(inputStr.substring(2,4),10);
    var dd = parseInt(inputStr.substring(4,inputStr.length),10);
    }
    if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) 
    {
    // there is a non-numeric character in one of the component values
    alert("日期格式错误。\n正确的格式为:yyyymmdd,yyyy/mm/dd/yyyy,或yyyy-mm-dd。");
    return false;
    }
    if (mm < 1 || mm > 12) 
    {
    // month value is not 1 thru 12
    alert("月份必须在01~12之间。");
    return false;
    }
    if (dd < 1 || dd > 31) 
    {
    // date value is not 1 thru 31
    alert("日期必须在01~31之间");
    return false;
    } // validate year, allowing for checks between year ranges
    // passed as parameters from other validation functions
    if (yyyy < 100) 
    {
    // entered value is two digits, which we allow for 1930-2029
    if (yyyy >= 30) 
    {
    yyyy += 1900;

    else 
    {
    yyyy += 2000;
    }
    } if (!checkMonthLength(mm,dd)) 
    {
    return false;
    }
    if (mm == 2) 
    {
    if (!checkLeapMonth(mm,dd,yyyy)) 
    {
    return false;
    }
    }
    // put the Informix-friendly format back into the field
    inputStr = yyyy + "-" + mm + "-" + dd;
    return true;
    }