判断输入的字符是否是数字

private bool isNumber(string s)
    {
        int Flag = 0;
        char[] str = s.ToCharArray();
        for (int i = 0; i < str.Length; i++)
        {
            if (Char.IsNumber(str[i]))
            {
                Flag++;
            }
            else
            {
                Flag = -1;
                break;
            }
        }
        if (Flag > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

测试代码:

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (isNumber(TextBox1.Text.Trim()))
        {
            DIV1.InnerHtml = """;
            return;
        }
        else
        {
            DIV1.InnerHtml = "";
            return;
        }
    } 

你可能感兴趣的:(asp.net)