function CardNumber(num, isVisa, isMaster) {
    var card = new Array();

    if (document.getElementById('radioVisa').checked) {
       card = isVisa;
    }  if  (isVisa){        
    ((isNaN(num))||(num[0] != '4') || (num.length != 16))
        return false;
    } else {
        return true;
    }

   if (document.getElementById.innerHTML('radioMaster').checked) {
       card = isMaster;
    } if  ((isNaN(num))||(num[0] != '51') || (num[0] != '52') || (num[0] != '53') || (num[0] != '54') ||(num[0] != '55') || (num.length != 16)) {
        return false;
    } else {
        return true;
    }

}

I am trying to validate a credit card, either visa or mastercard.... with what i have above, visa isn''t working and if i input a visa card number when mastercard is selected will return true... but if i put in a mastercard number when mastercard is selected it returns false..... Not sure what I am doing wrong?

Recommended Answers

All 4 Replies

There are few changes that you need to do.

  1. Instead of using array, use string.
  2. If you select one radio, you can select second also. Change the name of radio button. Both radio must have same name to make sure only one is checked. I have not done as working directly on mobile now.
  3. There must be 'and' between conditions for check of master card for starting two characters.

         <!DOCTYPE html>
         <html>
         <head>
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <link href="mycss.css" rel="stylesheet" type="text/css" />
    
           <title>validation</title>
        <script type="text/javascript">
        function validateCard(num,isVisa,isMaster){
              if(document.getElementById("radioVisa").checked)
        {
        card = isVisa;
                                           if(isNaN(num)||num.substring(0,1)!=('4')||num.length!=16)
        {
        alert('error in visa');
        return false;
        }
        else
        {
        alert(' no error in visa');
        return true;
        }
        }
        if(document.getElementById("radioMaster").checked)
        {
        if(isNaN(num)||(num.substring(0,2)!=('52')&&num.substring(0,2)!=('53')&&num.substring(0,2)!=('54')&&num.substring(0,2)!=('55'))||num.length!=16)
        {
        alert('error in master');
        return false;
        }
        else
        {
        alert(' no error in master');
        return true;
        }
        }
        }
    </script>
         </head>
      <body>
     <input type="radio" id="radioMaster" onclick="validateCard('5834567890123456',1,1)">Master</input>
      <input type="radio" id="radioVisa" onclick="validateCard('4234567890123456',1,1)">Visa</input>
       </body>
           </html>
    

@IIM Thank you for you help and also explaining the process to me, it does make it easier for me to learn. :)

@Jane: you are always welcome.
Also please Mark the thread as solved if your problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.