Forum Moderators: open
What I'm trying to say is if the first digit of ccn is 4, than the length of ccn must be equal to 13 or 16, else an error will alert.
else if((/^4/.test(ccn))&&(!(ccn.length==13)¦¦(ccn.length==16))){alert("Wrong Length!");}
I was able to get similar lines of code to work when only one length was accepted, but this 13 or 16 thing is messing me up.
Any advice would be greatly appreciated.
Thanks
The logical negation (!) only applied to the first within the or (¦¦).
This one works:
if( /^4/.test(ccn) &&!(ccn.length==13 ¦¦ ccn.length==16) ) You could smarten it up a little, using a trick that's especially good if you want to add extra acceptible lengths:
if( /^4/.test(ccn) &&!{13:1,16:1}[ccn.length] ) ...or sticking with RegExps, and using them on number type (which is allowed), the second test could be
!/^(13¦16)$/.test(ccn.length)
This line is apart of several to verify the credit card length.
I had one line for each of the four major credit cards, but was only having problem's with only one line because it could be 13 or 16 digits long, but now it looks like I am able to minimize all four lines into a concise regex statement.
Basically the credit card lengths follow this rule:
If the card starts with "3" (American Express) it must be 15 digits long.
If the card starts with "4" (VISA) it must be 13 or 16 digits long.
If the card starts with "5" (Master Card) it must be 16 digits long.
If the card starts with "6" (Discover Card) it must be 16 digits long.
Have a look around [google.se].
[breakingpar.com...]