# Credit Card Validation

I am currently checking what value the user types, and if they type the first number of any of these credit cards (3,4,5,6) it will change the icon to the type of card they are processing.

I have this formula as the icon:

IF(self.inputVal <= 3, “cc-amex”, IF(self.inputVal <= 4, “cc-visa”, IF(self.inputVal <= 5, “cc-mastercard”, IF(self.inputVal <= 6, “cc-discover”, IF(self.inputVal <= 2, “times”,“times”)))))

This works as expected, however, when I finish typing the credit card number, it changes the icon to times, which I knew would happen.

I was thinking about stripping all characters and only using the first digit, but how can I accomplish that in appgyver?

Well this has currently evolved to:

IF(self.inputVal <= 29999999999999, “credit-card-alt”, IF(self.inputVal <= 399999999999999, “cc-amex”, IF(self.inputVal <= 4999999999999999, “cc-visa”, IF(self.inputVal <= 5999999999999999, “cc-mastercard”, IF(self.inputVal <= 6999999999999999, “cc-discover”,“credit-card-alt”)))))

Which works for what im doing, however until the final digit is typed, it could show the incorrect card type.

Hi!

For the formula I would probably use SUBSTRING.

Also (you may already be doing everything carefully and with consideration but just in case) since you’re handling users’ credit cards, please be careful how you handle data in your app. We have a primer regarding security available here.

1 Like

Hello Zach…

Please correct me if I am wrong here but what you are doing is basically asking if the number is less than or equal to 3… and if it is such: it is “cc-amex”…
BUT in the end of your final IF statement: you state that if it is less than or greater to 2 it is “times”… ALSO that if it is incompatible to all IF statements: it is yet again: “times”

Why this is sort of odd for me is that IF 2, 3, 4, 5, 6 is the only possible outcomes: then wouldn’t it be best to start with 6 and leave out 2 (As final falseValue shall others not have been met) or to start with 2 and leave out 6? Since 2 is ALSO lesser than or equal to three (Unless there is a coding situation I am missing out on If I type in 1 I get “cc-amex”… If I type 2 I get “cc-amex” and 3 again “cc-amex”…
This sort of renders the last IF(self.inputVal <= 2,
“times” satement falicious so such 2 (or less) will never give you “times”… Only numbers greater than 6 will.

And THAT would be possibly why you get a:

situation

Now as I get it… You have a card number (16 digits) and you wnat to take the first digit off of it to evaluate.
Whereas this number would determine the bank:

• 2 → Times
• 3 → amex
• 4 → visa
• 5 → mastercard
• 6 → discover

Am I right?

IF so… Wouldn’t you be best off to isolate the first digit before playing the `IF` game?
From the top of my head I would say “Divide it by 1000000000000000” (as inconvenient as that may sound…)… `FLOOR` it to round it down to the nearest integer and then plug it into the IF loops excluding the “Lesser than” sign.
Hmmm wait… In retrospect… You would I presume want “the card” available from start onward right?
So even in the first number: You would like it visible… Wait… Then best we divide it by a 10 to the power of the number of digits in the variable… Something like:

``````FLOOR(self.inputVal / POWER(10, LENGTH(STRING(self.inputVal)) - 1 ))
``````

This should give you ALWAYS the first number how many numbers ever you type into the field…
`STRING` reads the text value of the input where `LENGTH` counts the letters in it (Hence why we made it a string)… `POWER` takes the first value and gives it the power of the second value… `FLOOR` takes the lower integer of the division.
(The additional “-1” is so that you get a number greater than 1… Like when you have a three digit number it will be divided by ten to the power of 3… Making it 0,1)

**Sort of LONG but well… like this (GIVEN that your variable is a NUMBER!!) ** might actually work for you…

``````IF(
FLOOR(self.inputVal / POWER(10, LENGTH(STRING(self.inputVal)) - 1 )) == 2,
"times",
IF(
FLOOR(self.inputVal / POWER(10, LENGTH(STRING(self.inputVal)) - 1 )) == 3,
"cc-amex",
IF(
FLOOR(self.inputVal / POWER(10, LENGTH(STRING(self.inputVal)) - 1 )) == 4,
"cc-visa",
IF(
FLOOR(self.inputVal / POWER(10, LENGTH(STRING(self.inputVal)) - 1 )) == 5,
"cc-mastercard",
IF(
FLOOR(self.inputVal / POWER(10, LENGTH(STRING(self.inputVal)) - 1 )) == 6,
"cc-discover",
"Error"
)
)
)
)
)
``````

LOL now that I finished the typing and copy/pasting of values… MAN does that look goofy… LONG and pretty wrong (THOUGH it SHOULD work!!)
And only AS I finished typing I realized that theoretically I could have just converted it to a `STRING` and then `TRUNCATE` 'ed it

I am a certified Idiot
I so hope this was helpful or at least entertaining

WHAT?!!!
When did these replies come from?
Guess I had a network issue of sort

Man did I make a fool of myself…

Well the girl knows her thing for sure…
I am so sorry dearest for not realizing the issue dealt with by a much more capable one

Maybe I spent too much time typing…
Hope you guys had some smiles at least though

2 Likes