I have been testing and googling but still could'nt work out pattern to validate comma separated numbers.
9 digits long, numbers only, no spaces, leading number for each digit cannot be zero
Tried
^(?:\s*\d{9}\s*(?:,|$))+$
but no go
Note that commas are required since the input file should between 3 up to 20 (max) comma separated integers
preg_match('=^[1-9][0-9,]*$=', $x);
should do it, unless you're saying the number must be 9 digits long (with optional commas? or are the commas required?), in which case try something like preg_match('=^[1-9][0-9]{2},?[0-9]{3},?[0-9]{3}$=', $x);
You mentioned that there should be no spaces, but you used \\s*
(0+ whitespaces) in your pattern. Also, (?:,|$)
matches a ,
or end of string, so your pattern allows a trailing ,
.
I suggest using
^[1-9]\d{8}(?:,[1-9]\d{8}){2,19}$
See the regex demo
Details :
^
- start of string [1-9]
- the first digit of the 9-digit number cannot be zero, 1
to 9
only \\d{8}
- the 8 remaining digits of the number (?:,[1-9]\\d{8}){2,19}
- 2 to 19 (in total, 3 to 20) occurrences of
,
- comma [1-9]\\d{8}){2,19}
- see above $
- end of string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.