i want check input by regex | contain 16 number
I have input, I want to check it.
It should accept only numbers, and must be 16, no more and no less.
开发者_StackOverflow社区How I can do that using php and regex?
Regex would be ^\d{16}$
preg_match("/^\\d{16}$/", $str);
^\d{16}$
Explication:
^
for the beginning of the line (so nothing before that)\d
meaning a number (decimbal){16}
to mean "exactly 16"$
for the ending of the line (so nothing after that)
I think you need it for the credit card validator
^(\d{4}[- ]){3}\d{4}|\d{16}$
this will match :
1234-1234-1234-1234 | 1234 1234 1234 1234 | 1234123412341234
Please note that this is copied from regexlib
Would it not be better to make sure its 16 characters and numbers only with is_numeric()
and strlen()
.
精彩评论