Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate a string is non-negative whole number in javascript

This is a solution for validating an integer. Can someone please explain the logic of Karim's answer.
This works perfectly, but i am not able to understand how.

var intRegex = /^\d+$/;
if(intRegex.test(someNumber)) {
   alert('I am an int');
   ...
}
like image 627
Lalithesh Avatar asked Jun 05 '13 13:06

Lalithesh


1 Answers

The regex: /^\d+$/

^ // beginning of the string
\d //  numeric char [0-9]
+ // 1 or more from the last
$ // ends of the string

when they are all combined:

From the beginning of the string to the end there are one or more numbers char[0-9] and number only.

like image 71
gdoron is supporting Monica Avatar answered Oct 09 '22 00:10

gdoron is supporting Monica