0

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?

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78

2 Answers2

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.

Community
  • 1
  • 1
weeska
  • 439
  • 3
  • 7