i have a text box and I need to allow only numbers,using ^[0-9]$ will not work perfectly, if we enter only white spaces, page.is valid becomes true how is it possible?
Asked
Active
Viewed 106 times
-2
-
Possible duplicate of [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) – NotTelling Mar 14 '17 at 11:42
-
@Istanfin - a winforms question is unlikely to produce a good answer for an [tag:asp.net] question – Damien_The_Unbeliever Mar 14 '17 at 11:47
-
1[RegularExpressionValidator](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator(v=vs.110).aspx#Remarks): "Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control." – Damien_The_Unbeliever Mar 14 '17 at 11:49
1 Answers
0
You can specify the pattern correctly as
ValidationExpression="^[0-9]*$"
^ Anchors the regex at the start of the string.
[0-9]* Matches zero
or more characters $ Anchors the regex at the end of the string.

Prashant-Systematix
- 269
- 2
- 5
-
True, but it doesn't answer the question: why does validation succeed for a whitespace-only (or empty) textbox? – Hans Kesting Mar 14 '17 at 13:04