How do I test whether a string contains only numbers and spaces using RegEx?
i.e.
- 021 123 4567 is valid
- 0211234567 is valid
- 0211 234 567 is valid
Pretty loose, but this meets my requirements.
Suggestions?
How about /^[\d ]+$/
? If it needs to start and end with a digit, /^\d[\d ]*\d$/
should do it.
[0-9]
<-- this means any single digit between 0 and 9[0-9 ]
<-- this means any single digit between 0 and 9, or a space[0-9 ]{1,}
<-- this means any single digit between 0 and 9, or a space, 1 or more times[0-9 ]{1,9}
<-- this means any single digit between 0 and 9, or a space, 1 to 9 times
n of digits with any number of spaces:
^(\s*\d\s*){11}$
here n=11; Test: here
Something like:
\d*|\s+^[A-Za-z]
精彩评论