2

I am trying to limit the input of a textbox of a very simple Web Application so that the user can only input numbers. Im having trouble doing this without using 20 lines of code, any help at all is appreciated!

protected void Button1_Click(object sender, EventArgs e)
    {
        {
            int input = int.Parse(InputBox.Text);
            if (input > 15)
            {
                String hot;
                hot = "hot";
                Result.Text = hot;
                Result.BackColor = System.Drawing.Color.Red;
            }
            else if (input <= 15)
            {
                String cold;
                cold = "cold";
                Result.Text = cold;
                Result.BackColor = System.Drawing.Color.Blue;
            }
        }
    }

Thank you!

4

7 回答 7

2

以下是验证数值的 javascript 代码。

<script type="text/javascript">
        function ValidateTextBox() {
            var value = document.getElementById("txtprice").value;
            if (!isNumber(value)) {
                alert("Please Enter Numeric Value ");
                return false;
            }
            else {
                return true;
            }
        }
        function isNumber(n) {
            return !isNaN(parseFloat(n)) && isFinite(n);
        }
    </script>

ASPX 页面:-

  <asp:Button ID="Button1" runat="server" OnClientClick="return ValidateTextBox();" Text="Button" OnClick="Button1_Click" />
于 2013-11-04T12:47:05.150 回答
0

Try adding the following markup to your Web Application next to your TextBox :

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Input must be a number!"
    ValidationExpression="^[1-9]\d*$" ControlToValidate="InputBox"></asp:RegularExpressionValidator>

This will validate the control "InputBox" on every postback to ensure it matches the regular expression ^[1-9]\d*$, which only allows Integer values.

If you only want a specific postback to validate the input look at Validation Groups.

Also if you require decimal values or need to allow other numeric values see the following questions Answer : https://stackoverflow.com/a/2811058/1941466

于 2013-11-04T12:49:07.317 回答
0

在将表单发送到服务器之前,您还可以使用 HTML5 表单验证进行一些验证。以下是一些示例(http://www.the-art-of-web.com/html/html5-form-validation/):

<input type="text" name="temp" required> (requierd validation)
<input type="number" size="2" name="temp" min="-273" max="999" value="21"> (number input type with defaults and limits)

有了 HTML5 验证,您就不需要验证代码中的任何内容,您可以按原样使用您的代码。(注意:在实际生产代码中,我会验证客户端和服务器端)

于 2013-11-04T12:52:38.137 回答
0

要在用户的 html 页面上限制它,您需要显式或隐式使用 Javascript 或 jQuery 函数(例如通过微软工具)。要通过在服务器端将文本值分隔成行来获取行数,请使用 String.Split 方法。

于 2013-11-04T12:39:50.183 回答
0

使用正则表达式检查它是否是数字,然后用它做你需要的,像这样:

  if ((Regex.IsMatch(InputBox.Text, @"^[0-9]+$")))
        {
            try
            {
于 2013-11-04T12:41:25.970 回答
0

使用int.TryParse,这将返回一个布尔值,具体取决于字符串是否可以解析为 int。这样您就可以知道是否在文本框中输入了数字。

于 2013-11-04T12:41:29.473 回答
0

您是否尝试过在网页上使用正则表达式来防止用户输入数字?也许 Try catch 也可能会有所帮助

try{
    int input = int.Parse(InputBox.Text);
    //add any addition logic you need here
}
catch{
    Result.Text="Not a number";
}
于 2013-11-04T12:43:18.790 回答