Check asp.net to make sure the text field has integer values

I have the required verification setting in the text box, but I have to make sure that this is also an integer.

How can i do this?

+46
validation
Sep 15 '09 at 14:43
source share
10 answers

If all that bothers you is that the field contains an integer (i.e. not related to a range), add a CompareValidator with it Operator property set to DataTypeCheck :

 <asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="ValueTextBox" ErrorMessage="Value must be a whole number" /> 

If there is a certain range of values โ€‹โ€‹that are valid (maybe there are), you can use RangeValidator , for example

 <asp:RangeValidator runat="server" Type="Integer" MinimumValue="0" MaximumValue="400" ControlToValidate="ValueTextBox" ErrorMessage="Value must be a whole number between 0 and 400" /> 

They will only check if there is text in the TextBox, so you also need to leave RequiredFieldValidator .

Like @Mahin , make sure that you check the Page.IsValid property on the server side, otherwise the validator works only for users with JavaScript enabled.

+99
Sep 15 '09 at 15:08
source share

This works fine for me:

 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="YourTextBoxID" ErrorMessage="Only numeric allowed." ForeColor="Red" ValidationExpression="^[0-9]*$" ValidationGroup="NumericValidate">* </asp:RegularExpressionValidator> 

I think you should add ValidationGroup="NumericValidate" to your submit button.

+6
Mar 13 '13 at 21:59
source share

Use Int32.TryParse.

  int integer; Int32.TryParse(Textbox.Text, out integer) 

It will return a bool so you can see if they entered a real integer.

+4
Sep 15 '09 at 14:46
source share

Attach the regular expression checker to the text box and make its expression as follows:

 ^\d+$ 

And of course, do your server side validation.

+2
Sep 15 '09 at 14:46
source share

There are several ways you can handle it. You can add RequiredFieldValidator as well as RangeValidator (if that works for your case), or you can add CustomFieldValidator.

Link to CustomFieldValidator: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator%28VS.71%29.aspx

Link to the MSDN article on ASP.NET Validation: http://msdn.microsoft.com/en-us/library/aa479045.aspx

+2
Sep 15 '09 at 14:48
source share

http://msdn.microsoft.com/en-us/library/ad548tzy%28VS.71%29.aspx

When using validator validation elements, you should be careful that anyone can disable javascript in their browser. Therefore, you should use the Page.IsValid property always on the server side.

+1
Sep 15 '09 at 14:51
source share
 <script language="javascript" type="text/javascript"> function fixedlength(textboxID, keyEvent, maxlength) { //validation for digits upto 'maxlength' defined by caller function if (textboxID.value.length > maxlength) { textboxID.value = textboxID.value.substr(0, maxlength); } else if (textboxID.value.length < maxlength || textboxID.value.length == maxlength) { textboxID.value = textboxID.value.replace(/[^\d]+/g, ''); return true; } else return false; } </script> <asp:TextBox ID="txtNextVisit" runat="server" MaxLength="2" onblur="return fixedlength(this, event, 2);" onkeypress="return fixedlength(this, event, 2);" onkeyup="return fixedlength(this, event, 2);"></asp:TextBox> 
+1
Dec 12 '12 at 13:25
source share

You can use java script for this: -

 <asp:TextBox ID="textbox1" runat="server" Width="150px" MaxLength="8" onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;"></asp:TextBox> 
+1
Aug 27 '13 at 6:57
source share

Visual Studio now has integrated support for range checking and type checking: -

Try the following: - TO CHECK THE RANGE Before checking / checking a specific range of numbers Go to the design view from the layout view. Then: -

View> Toolbar> Validation

Now drag on RangeValidator to the design page where you want to display the error message (if the user enters a range value) now click on the RangeValidator control. Right click and select properties. In the Properties window (usually open under the solution bar), select ERROR MESSAGE. Write: -

The number must be in the range.

Now select in the control to check and select your TextboxID (or record it anyway) from the drop-down list. Locate Enter yourself in the property bar and select Integer down.
A little higher you will find the maximum and minimum value. Enter your desired number.

For type checking (without any range)
Before Validating / Verifying a Specific Range of Numbers Go to the design view from the layout view. Then: -

View> Toolbar> Validation

Now drag it onto CompareValidator onto the design page where you want to display the error message (if the user enters some text into it). now click on the CompareValidator control. Right click and select properties. In the Properties window (usually open under the solution bar), select ERROR MESSAGE. Write: -

The value must be a number.

Now find the option ControltoValidate and write the name controlID in it (alternatively, you can also choose from the drop-down list). Set the Operator option and write DataTypeCheck (alternatively, you can also choose from the drop-down list) in it. Find the Type option and write Integer in it.

Sit.

Alternatively, you can write the following code on an aspx page: <% - to check without any range -%>

+1
Jul 08 '14 at 12:11
source share

Double click the button and use the following code: -

 protected void button_click(object sender,EventArgs e) { int parsedValue; if(int.TryParse(!txt.Text,out parsedValue)) { Label.Text = "Please specify a number only !!"; //Will put a text in a label so make //sure //you have a label } else { // do what you want to } 
0
Jul 14 '14 at 10:46
source share



All Articles