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!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
WhereAreYouSyntax
  • 163
  • 2
  • 4
  • 13
  • That doesn't limit the input of the textbox to numbers, you are just checking the value given against numbers. Which could cause this to fall over. Do you want to **limit** the input or just check values? – Nunners Nov 04 '13 at 12:36
  • I have removed my attempt at limiting it because it was a mess of code :/ I want to limit the input (and then check the values) – WhereAreYouSyntax Nov 04 '13 at 12:38
  • you can use regular expression for that – Sai Nov 04 '13 at 12:41
  • See my answer below, gives you exactly what you want. – PmanAce Nov 04 '13 at 13:05

7 Answers7

2

following is the javascript code to validate numeric value.

<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 Page:-

  <asp:Button ID="Button1" runat="server" OnClientClick="return ValidateTextBox();" Text="Button" OnClick="Button1_Click" />
Kuldeep
  • 454
  • 1
  • 3
  • 11
0

To limit it at user's html page you need use Javascript or jQuery functions explicitly or implicity (e.g. via microsoft tools). To get lines count by separating text value into lines on server side use String.Split method.

LINQ2Vodka
  • 2,996
  • 2
  • 27
  • 47
0

Use a regular expression to check if it is numeric, then do what you need with it after, like this:

  if ((Regex.IsMatch(InputBox.Text, @"^[0-9]+$")))
        {
            try
            {
smoore4
  • 4,520
  • 3
  • 36
  • 55
0

Use int.TryParse, this returns a boolean depending on whether the string can be parsed to an int. This way you'll know if numbers have been entered into the textbox.

James
  • 2,195
  • 1
  • 19
  • 22
0

Have you tried using regex on the webpage to prevent the user from entering numbers? Perhaps a Try catch may also help like

try{
    int input = int.Parse(InputBox.Text);
    //add any addition logic you need here
}
catch{
    Result.Text="Not a number";
}
Zane Chung
  • 154
  • 6
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

Community
  • 1
  • 1
Nunners
  • 3,047
  • 13
  • 17
0

You can also use HTML5 form validation to do some validation before sending your form to the server. Here are some examples (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)

With that HTML5 validation in place, you wouldn't need to validate anything in your code, you could use your code as is. (Note: in real production code, I would validate client and server side)

PmanAce
  • 4,000
  • 2
  • 24
  • 29