0
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?

3
Contributors
4
Replies
23
Views
4 Years
Discussion Span
Last Post by ryantroop
1

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>
    

Edited by IIM

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.