Reputation: 43
I am trying to validate some forms and what I want to have is a regular expression that will allow numbers and spaces. The expression will not allow
1000 space space 00
but it will allow something like
space space space 10000
or
1000 space space space
I have tried this, but it's not what I want:
var filter = /^[0-9 ]+$/;
if(price.val() == "")
alert('vide');
else if(filter.test(price.val()))
alert('number');
else
alert('no number');
Hope you can help me.
Upvotes: 0
Views: 196
Reputation: 31035
You can use a regex like this:
^\s*\d+\s*$
Regex explanation from Hwnd tool:
^ the beginning of the string
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times)
\d+ digits (0-9) (1 or more times)
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times)
$ before an optional \n, and the end of the
string
Upvotes: 1