I want to validate 'int' field with javascript regular expression.
I am using this RE string
var numbers =/^[0-9]+$/;
This expresion does not allow spaces in the text box.
How do I create a regular expression which allows for spaces in the text box?
I want to validate 'int' field with javascript regular expression.
I am using this RE string
var numbers =/^[0-9]+$/;
This expresion does not allow spaces in the text box.
How do I create a regular expression which allows for spaces in the text box?
Share Improve this question edited Mar 21, 2013 at 5:00 Rob Di Marco 45k9 gold badges71 silver badges57 bronze badges asked Mar 21, 2013 at 4:59 user2110346user2110346 251 silver badge8 bronze badges 1- What do you mean by "allows for spaces"? The answers so far demonstrate that this can be interpreted in several (inpatible) ways. – Ted Hopp Commented Mar 21, 2013 at 5:22
5 Answers
Reset to default 5Add optional spaces with:
var numbers =/^\s*[0-9]+\s*$/;
Add a space to the character set:
/^[0-9 ]+$/
If you want to guarantee that at least one digit exists, you could use a lookahead:
/^(?=\s*\d)[\d\s]+$/
This regex allow the numbers and spaces.
/[^\d\s]/
It depends where you want to allow spaces and whether you want input of spaces only (no digits) to be allowed. If you want to require at least one digit and any distribution of spaces, you can do this:
/^(\s*\d+)+\s*$/
This will allow input such as " 202 555 1212 "
This will permit to use spaces, tabs and digits, but require to use at least one digit:
^\s*\d[\d\s]*$
Online fiddle.re demo.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742284468a4415078.html
评论列表(0条)