-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,319
  • 1
  • 33
  • 40
Tarsem Singh
  • 37
  • 1
  • 9

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
  • 339
  • 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
  • 53,759
  • 13
  • 140
  • 188
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,919
  • 1
  • 21
  • 22