Forum Moderators: open

Message Too Old, No Replies

If first number is 4 than length must be 13 or 16

         

Jeremy_H

8:41 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



I am having a devil of a time getting this line of code to work.

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

Bernard Marx

11:35 pm on Jun 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I really knew how to peel my onions I could knock up a single RegExp to do this (one that isn't 50 chars long).

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)

Jeremy_H

12:50 am on Jun 30, 2006 (gmt 0)

10+ Year Member



Thanks!

That line of code was really messing me up.

I really like the regex code.

Thanks.

jshanman

1:02 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



How to peel onions...

Digits only (4 followed by 12 or 15 other digits):
/4[0-9]{12}$¦4[0-9]{15}$/.test(ccn)

Any string (4 followed by ANY 12 or 15 other characters):
/4.{12}$¦4[0-9]{15}$/.test(ccn)

Hope that helps.
- JS

Jeremy_H

4:02 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



Thanks,

Thats a great improvement.

Real quick, would it be better to add a carrot to signify the start of the string?

/^4.{12}$¦4[0-9]{15}$/.test(ccn)

Jeremy_H

4:18 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



I was all excited, thinking I could significantly reduce some of my code with the following line, but it seems to keep tripping on something.

else if(/^3.{14}$¦^4.{12}$¦^4.{15}$¦^5.{15}$¦^6.{15}$/.test(ccn)){alert("Wrong Length.");return false;}

jshanman

4:23 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



paste some of the strings that your are testing so I can understand the point of this...

- JS

Jeremy_H

5:08 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



I'm creating a script that will run on a form's onSubmit to verify some of the data before sending it to the server.

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.

Bernard Marx

5:24 pm on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe..

[b]/^(3\d{15}¦4\d{13}¦[456]\d{16})$/[/b]

jshanman

5:29 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



OK, so they have to be numbers...
/^3[0-9]{14}$¦^4[0-9]{12}$¦^[456][0-9]{15}$/.test(ccn)

That should work.
- JS

Bernard Marx

5:30 pm on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



However, there are a few things worth considering, such as removing all whitespace from the string first. There are further validations that could be made if you feel like it. Something like checksums.

Have a look around [google.se].
[breakingpar.com...]