I removed the {12,} and now a set of 16 digits doesn't validate and returns an error?
Because you removed the
quantifier. Your result was then, roughly, "begins and ends with ONE character, a number, dash, or space."
^[\d\-\s]$ Begins, contains only, and ends with ONE of these characters
^[\d\-\s]*$ Begins, contains only, and ends with zero or more of these characters (worthless in this case)
^[\d\-\s]+$ Begins, contains only, and ends with one or more of these characters
^[\d\-\s]{12,16}$ Begins, contains only, and ends with 12 to 16 of these characters (also really not very helpful, it could match on 16 dashes or 16 spaces)
I had to take out the ! at the front, and I don't even really know why.
! is
not, so "if it doesn't match, error" turns into "if it matches, OK, do this."
if (!preg_match(expression)) {
echo "doesn't match";
}
if (preg_match(expression)) {
echo "it matches on something, though I'm not sure what. :-)";
}
The only useful exercise for credit card numbers -because they vary in length based on what type of card it is - would have to include a card type and if statements. See E's link on advanced CC regexes for a taste.