Logo

Programming-Idioms

History of Idiom 137 > diff from v6 to v7

Edit summary for version 7 by programming-idioms.org:
New JS implementation by user [programming-idioms.org]

Version 6

2016-06-10, 21:55:21

Version 7

2016-06-10, 22:05:54

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Code
var b = /^[0-9]*$/.test(s);
Comments bubble
Notice the ^ and $ for "beginning of string" and "end of string".
Origin
http://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits