javascript - How to allow space in numeric regular expression? - Stack Overflow

I want to validate 'int' field with javascript regular expression.I am using this RE string v

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
Add a ment  | 

5 Answers 5

Reset to default 5

Add 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信