请问我任何让TextBox的输入格式为yyyy-MM-dd,否则不让输(只是不让输而不是提示)。

解决方案 »

  1.   

    javascript
    function CheckDate(strValue)
    {
    var month = new Array(1, 3, 5, 7, 8, 10, 12);
    var strTemp = strValue.split('-');
    if(strTemp.length != 3)
    return false;
    var day = strTemp[2];
    var mon = strTemp[1];
    var year = strTemp[0];

    if(mon > 12)
    return false;

    if((day.length == 0) || (day.length >2) || (parseInt(day,10) == 0))
    return false;

    for(var i = 0; i < day.length; i++)
    {
    var ch = day.charAt(i);
    if(ch < '0' || ch > '9')
    return false;
    }

    for(var j = 0; j < month.length; j++)
    {
    if(mon == month[j])
    {
    if(day > 31|| day <1)
    return false;
    else return true;
    }
    }

    if(mon != 2)
    {
    if(day > 30 || day < 1)
    return false;
    return true;
    }
    if( ((year %4 == 0)&&(year %100!=0)) ||(year %400==0))
    {
    if(mon == 2)
    {
    if(day >29|| day <1)
    return false;
    }
    }
    else

    if(mon == 2)
    {
    if(day > 28|| day < 1)
    return false;
    }
    }
    return true; 
    }在你 keydown 之后判断,如果返回 false
    document.getElementById("ID").value = document.getElementById("ID").value.substring(0, document.getElementById("ID").value.length - 1);
      

  2.   

    <script language="JavaScript">
    <!--
    function CheckDate(strDate)
    {
    var reg=/^(\d{4})([-])(\d{2})([-])(\d{2})/;
    if(!reg.test(strDate))
    {
    alert("日期格式不正确!\n正确格式为:2004-01-01");
    return false;
    }
    var ss=strDate.split("-");
    var year=ss[0];
    var month=ss[1];
    var date=ss[2];
    if(!checkYear(year))
    {
    return false;
    }
    if(!checkMonth(month))
    {
    return false;
    }
    if(!checkDate(year,month,date))
    {
    return false;
    }
    return true;
    } function checkYear(year)
    {
    if(isNaN(parseInt(year)))
    {
    alert("年份输入有误,请重新输入!");
    return false;
    }
    else if(parseInt(year)<1950 || parseInt(year) >2050)

    alert("年份应该在1950-2050之间!");
    return false;
    }
    else return true;
    } function checkMonth(month)
    {
    if(isNaN(parseInt(month)))
    {
    alert("月份输入有误,请重新输入!");
    return false;
    }
    else if(parseInt(month)<1 || parseInt(month) >12)
    {
    alert("月份应该在1-12之间!");
    return false
    }
    else return true;
    } function checkDate(year,month,date)
    {
    var daysOfMonth=CalDays(parseInt(year),parseInt(month));
    if(isNaN(parseInt(date)))
    {
    alert("日期输入有误,请重新输入!");
    return false;
    }
    else if(parseInt(date)<0||parseInt(date)>daysOfMonth)

    alert("日期应该在1-"+daysOfMonth+"之间!");
    return false;
    }
    else return true;
    } function CalDays(year,month)
    {
    var date= new Date(year,month,0);
    return date.getDate();
    } function isLeapYear(year)
    {
    if((year %4==0 && year %100!=0) || (year %400==0))
    return true;
    else return false;
    }
    alert(CheckDate("2007-02-29"));
    //-->
    </script>
      

  3.   

    我现在项目里面的做法,是把TextBox设为只读,然后再拉一个日历控件,让用户只能从日历控件,选择输入.
      

  4.   

    只能 chenyuming2004(这辈子我算是废了) 说的那样做了,