如要求用户输入的日期必须在1950-01-01到207-12-31的范围之内

解决方案 »

  1.   

    试试看:
    function isDateString()

    var sDate=document.Foo.Date.value;
    var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
    var iaDate = new Array(3)
    var year, month, day

    iaDate = sDate.toString().split("-")
    if (iaDate.length != 3) {alert("非法的日期"); return false;}
    if (isNaN(iaDate[0]) || isNaN(iaDate[1]) ||isNaN(iaDate[2])) {alert("非法的日期"); return false;}
    if (iaDate[0].length<1 || iaDate[0].length>4 
    || iaDate[1].length<1 || iaDate[1].length > 2 
    || iaDate[2].length <1 || iaDate[2].length>2) {alert("非法的日期"); return false;}

    year = parseFloat(iaDate[0])
    month = parseFloat(iaDate[1])
    day=parseFloat(iaDate[2]) if (year < 1950 || year > 2007) {alert("非法的日期"); return false;}
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
    if (month < 1 || month > 12) {alert("非法的日期"); return false;}
    if (day < 1 || day > iaMonthDays[month - 1]) {alert("非法的日期"); return false;}

    alert("合法!");
    return true;
    }