如何判断用户输入的是字母还是数字?用vb.net写

解决方案 »

  1.   

    "[\d]"--数字
    "[a-zA-Z]" -- 字母正则表达式.
      

  2.   

    数字要求时整数,不能为小数了,需要是int型的
      

  3.   

    <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
    <asp:Button id="Button3" runat="server" Text="数字OR字母"></asp:Button><asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
    <asp:Button id="Button3" runat="server" Text="数字OR字母"></asp:Button>
    private void Button3_Click(object sender, System.EventArgs e)
    {
    string str = this.TextBox2.Text.Trim();
    System.Text.RegularExpressions.Regex strRegex = new Regex(@"[0-9]",RegexOptions.IgnoreCase);
    //MatchCollection metches = strRegex.Matches(str);
    if(strRegex.Matches(str).Count == str.Length)
    {
    Response.Write("<br>输入的全是数");
    }
    System.Text.RegularExpressions.Regex strRegex2 = new Regex(@"[a-z]",RegexOptions.IgnoreCase);
    if(strRegex2.Matches(str).Count == str.Length)
    {
    Response.Write("输入的全是字母");
    } }
      

  4.   

    抱歉,我的第一个回答是错位的!
    "[\d]"--数字
    替换为
    "[/d]"--数字
    楼上的没有使用验证控件.
    如果是输入的时候验证,那可以使用服务器验证控件的.
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"
    ControlToValidate="TextBox1" ValidationExpression="[/d]+"></asp:RegularExpressionValidator>
    <asp:Button id="Button2" runat="server" Text="Button"></asp:Button>
    --------------
    ValidationExpression="[/d]+"至少一个数子.
    ValidationExpression="[a-zA-Z]+">至少一个字母