有个textbox 想用验证控件验证输入内容为数值型

解决方案 »

  1.   

    private bool IsNumeric(string str) 

    if (str==null || str.Length==0) 
    return false; 
    foreach(char c in str) 

    if (!Char.IsNumber(c)) 

    return false; 


    return true; 
    }
      

  2.   

       <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                         ControlToValidate="TextBox1"
                         ValidationExpression="\d"
                         Display="Static"
                         EnableClientScript="false"
                         ErrorMessage="必须为数字"
                         runat="server"/>
      

  3.   

    <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                        ControlToValidate="TextBox1" 
                        ValidationExpression="\d" 
                        Display="Static" 
                        EnableClientScript="false" 
                        ErrorMessage="必须为数字" 
                        runat="server"/> 
      

  4.   

            呵呵。。大家的都不错。。我来一个高效的。
                 public bool IsNumber(string txt)
    {
    bool returnVal = false;
    try
    {
    int.Parse(txt);//不是整数是报异常
    returnVal = true;
    }
    catch
    {}
    return returnVal;
    }
      

  5.   

    <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                        ControlToValidate="TextBox1" 
                        ValidationExpression="\d" 
                        Display="Static" 
                        EnableClientScript="false" 
                        ErrorMessage="必须为数字" 
                        runat="server"/> 
      

  6.   

    public bool IsNumber(string txt)
    {
    bool returnVal = false;
    try
    {
    int.Parse(txt);//不是整数是报异常
    returnVal = true;
    }
    catch
    {}
    return returnVal;
    }
      

  7.   

    只能输数字的,我有个好用的代码:
    onpaste="this.value=this.value.replace(/[^0-9]/g,'')"