<html>
<body>
<form name="form1">
<input size=8 type="text" name="a" onkeyup="check()" onchang="check()" onkeydown="check()">
</form>
</body>
</html>
<script language=javascript>
function check()
{   if(document.form1.a.value.length>8)
{
 alert("只能输入8位");
}
}
</script>

解决方案 »

  1.   

    TextBox.Changed事件里判断
    此事件在文本更改时发生public void Form1()
    {
    ...
    textBox1.Changed+=new EventHandler(Text_Changed);
    ...
    }private void TextChanged(object sender,EventArgs e)
    {
        if(textBox1.Text.Length>8)
        { 
          //在这里放置处理代码
         }
    }
      

  2.   

    .net不是有规则检验控件的么?填入正则表达式{d8}就可以了,不用写代码
      

  3.   

    用vs工具蓝里的reg打头的就是
      

  4.   

    你是做WinForm代码吗?如果是代码这样编写:
    在textBox的textChanged事件中写入代码应用using System.Text.RegularExpressions;
    用正则表达式限定输入0-9的数字
    private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
    Regex digitregex = new Regex("(^[0-9]{1,})");
    if (!digitregex.IsMatch(textBox1.Text))
    {
    MessageBox.Show("要求输入0-9之间的数字!");
    textBox1.Text = textBox1.Text.Substring(0,textBox1.Text.Length-1);//将最后输的一位删掉
    }else
    {
    if(this.textBox1 .Text.Length>8)
    {
    MessageBox.Show("输入数字不得超过8位!");
    }
    else 
    this.textBox1 = this.textBox1.Text.Substring(0,8);//如果输入到第9位,直接将第九位数删掉,截取前8位
    }
    }
      

  5.   

    你是做WinForm代码吗?如果是代码这样编写:
    在textBox的textChanged事件中写入代码
    引用用using System.Text.RegularExpressions;
    用正则表达式限定输入0-9的数字private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
        Regex digitregex = new Regex("(^[0-9]{1,})");
        if (!digitregex.IsMatch(textBox1.Text))
        {
           MessageBox.Show("要求输入0-9之间的数字!");
           textBox1.Text = textBox1.Text.Substring(0,textBox1.Text.Length-1);//将最后输的一位删掉
        }    else
       {
           if(this.textBox1 .Text.Length>8)
           {
               MessageBox.Show("输入数字不得超过8位!");
               this.textBox1 = this.textBox1.Text.Substring(0,8);//如果输入到第9位,直接将第九位数字删掉,截取前8位
           }
       }
    }
    不好意思上一个格式太乱了,重写一个吧!
    其实我也是菜鸟,不过不要紧,只要努力加油,我们也会象前辈们一样的!