I'm using a field with string datatype, I want add validation to that field which will check if the string entered into the field contains only numbers, please help me with the regex?
Asked
Active
Viewed 774 times
2 Answers
2
You can use the below given pattern for checking:
^(0|[1-9][0-9]*)$
If it returns true, it means that there are only digits inside the field, otherwise not.

Rajesh Omanakuttan
- 6,788
- 7
- 47
- 85
1
I'd check if \D+
returns a match. If so, there's at least one element that isn't a digit.
Also, Test if string is a number in Ruby on Rails gives some answers without using regular expressions.
-
1The above is not correct. E.g., if the pattern is like "1 2", because it will return true for white spaces also. – Rajesh Omanakuttan Sep 11 '13 at 20:15
-
But that's what's expected? – weeska Sep 11 '13 at 20:26