Logical Operators Issue

Guys,
I need your help with this piece of code, it is a simple way to detect the type of Credit Card entered while is being typed in… You can tell by looking at the First Digit, the First 2 Digits or the First 4 digits of it… Problem is, the logic below is not working as expected. Even if a type a blank space, it gives me “Amex” (!?)

What am I doing wrong!?
Any help will be appreciated,
-Thank you

export function CardNumber_KeyPress()
{
var FirstDigit = parseInt($w(“#CardNumber”).value.substr(0, 1),10);
var TwoDigits = parseInt($w(“#CardNumber”).value.substr(0, 2),10);
var FourDigits = parseInt($w(“#CardNumber”).value.substr(0, 4),10);

if (FirstDigit === 4) { 
     $w("#text176").text = "Visa" ;} 
  else if ((TwoDigits > 50 && TwoDigits < 56) || (FourDigits > 2220 && FourDigits < 2721)) { 
     $w("#text176").text = "Master" ;} 
  else if ((TwoDigits = 34) || (TwoDigits = 37)) { 
        $w("#text176").text = "Amex" ;} 
     else if ((TwoDigits = 64) || (TwoDigits = 65) || (FourDigits = 6011)) { 
        $w("#text176").text = "Discover" ;} 
     else { 
	    $w("#text176").text = "Undefined" ;} 

}

1 Like

I believe your equality checks should be using operator ===, not operator =.

1 Like

Hi,
sdvisconti is correct.
All of your equality checks such as (TwoDigits = 34) || (TwoDigits = 37)) are actually performing an assignment and not a check.
Meaning they make TwoDigits to be 34.
An assignment operation always returns the assigned value, therefore you’re actually checking ‘if(34)’, which is true.
use (TwoDigits === 34) || (TwoDigits === 37)). This will solve your problem.

Liran.

2 Likes

Of Course!!! How could I have missed that!?
I guess you don’t think clearly when you have 24 hours without sleep.

Thank you very much Liran and Sdvisconti
It is working now.

-Luigi

1 Like

This is pure gold :slight_smile: <3