C#里面怎样限制textBox控件里键入或粘贴的最小字符数最大字符数有MaxLength,那最小字符数呢??

解决方案 »

  1.   

    比如最少输入2位,   
     private void textBox1_Validating(object sender, CancelEventArgs e)
            {
                if (textBox1.Text.Length < 2)
                {
                    MessageBox.Show("error");
                    textBox1.Text = "";
                    textBox1.Focus();
                }
            }
      

  2.   


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    <script type="text/javascript">
    function CheckMinLength(Myobj)
    {
        var MyobjValue=Myobj.value;
        var length=MyobjValue.replace(/[^\x00-\xff]/g,'xx').length; //中文占两个字节的 所以 转换成xx两位的 再计算长度
        if(length<2)
        {
          alert('最小字节数不能小于2个!');                          //错误的提示方法你自己看着办不一定使用alert
          Myobj.focus();
        }
    }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
     <asp:TextBox ID ="txtName" runat="server" onblur="CheckMinLeng(this)"></asp:TextBox>
        </form>
    </body>
    </html>