asp net validation to make sure textbox has integer values

asp net validation to make sure textbox has integer values  using -',validation'

asked Sep 22, 2015 by deepak gupta
0 votes

6 Answers

0 votes

Use Int32.TryParse.

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

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

answered Sep 22, 2015 by r3tt
0 votes

If all that you are concerned about is that the field contains an integer (i.e., not concerned with a range), then add a CompareValidator with it's Operator property set to DataTypeCheck:

If there is a specific range of values that are valid (there probably are), then you can use a RangeValidator, like so:

These will only validate if there is text in the TextBox, so you will need to keep the RequiredFieldValidator there, too.

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

answered Sep 22, 2015 by ashishshukla
0 votes

Attach a Regular Expression Validator to the text box and make its expression be this:


And do your server side validation too, of course.

answered Sep 22, 2015 by gauravsinghal83
0 votes

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

Link to the CustomFieldValidator:

Link to MSDN Article on ASP.NET Validation:

answered Sep 22, 2015 by kinnari
0 votes

This works fine to me:


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

answered Sep 22, 2015 by loknath.ganji
0 votes

When using Server validator controls you have to be careful about fact that any one can disable javascript in their browser. So you should use Page.IsValid Property always at server side.

answered Sep 22, 2015 by girisha