Regex for field that allows numbers and spaces

后端 未结 5 2021
南方客
南方客 2020-12-11 00:13

I want that the text field only accept numeric values and spaces. What is regular expression for this? I was trying with \\d+$ Thanks

相关标签:
5条回答
  • 2020-12-11 00:43

    This should work.

    /^[0-9\s]*$/
    
    0 讨论(0)
  • 2020-12-11 00:44

    You're going to want to use [0-9 ]+

    0 讨论(0)
  • 2020-12-11 00:45

    ^ for the begining of string. [\d ]* for any combination of this symbols. $ for end of string.

    ^[\d ]*$

    0 讨论(0)
  • 2020-12-11 00:45

    If you want to match only the numbers, use:

    (\b\d+)/g or (\b[0-9]+)/g

    where:
    \b will match borders
    \d or [0-9] match numbers
    + will match 1 or more times \d (or [0-9])
    /g turns on global mode, to match the regex many times

    0 讨论(0)
  • 2020-12-11 00:54

    This one should work :

    [0-9 ]+
    
    0 讨论(0)
提交回复
热议问题