function check(txtobject){
    if (txtobject.value.length>10){
        alert("字符超长");
    }
}
<input name=txt type=text onkeyup="check(this)">

解决方案 »

  1.   

    <script>
    function check(txtobject)
    {
        if (txtobject.value.length>10)
        {
            alert("字符超长");
            txtobject.value=txtobject.value.substring(0,10);
        }
    }
    </script>
    <input name=txt type=text onkeyup="check(this)">
      

  2.   

    再改一改吧:)
    <script>
    function check(txtobject)
    {    if (txtobject.value.length>10)
        {
            //alert("字符超长");
            txtobject.value=txtobject.value.substring(0,10);
        }
    }
    </script>
    <input name=txt type=text  onpropertychange="check(this)">