user3851186
user3851186

Reputation: 43

regular expression for number and spaces in the end

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

Answers (3)

Aravind
Aravind

Reputation: 2198

Hope this pattern helps ^ *[0-9]+ *$

Upvotes: 0

Federico Piazza
Federico Piazza

Reputation: 31035

You can use a regex like this:

^\s*\d+\s*$

Working demo

enter image description here

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

Stryner
Stryner

Reputation: 7328

You could try

var filter = /^\s*[0-9]+\s*$/;

\s is the whitespace character. You can replace it with if you only want to specifically check for spaces.

Upvotes: 4

Related Questions