-1

I am trying to make regular expression that allow only numbers and must be mandatory. no blank field or white space allow using asp regular expression validater with C#. i tried this ValidationExpression="^[/d]*+$". this is working for number only. blank field accepted by this expression.

Thanks

Cheran Shunmugavel
  • 8,119
  • 1
  • 31
  • 39

3 Answers3

2

The RegexValidator isn't called when the field is empty, you must use the RequiredFieldValidator in combination : http://msdn.microsoft.com/en-us/library/eahwtc9e%28v=vs.100%29.aspx

Besides, the correct regex would be

^\d+$

\d is for any number

+ is for 1 to n occurences

Trajan
  • 329
  • 2
  • 15
2

Be careful that \d can match digits other than 0 to 9, such as Eastern Arabic numerals. I would suggest using:

"^[0-9]+$"
Community
  • 1
  • 1
Douglas
  • 52,041
  • 10
  • 126
  • 179
0

Take out the "*". The "+" is being applied to the results of the previous expression which is "0 or more digits" and is always found.

ValidationExpression="^[/d]+$"  
asantaballa
  • 3,768
  • 1
  • 19
  • 20