Print Page | Close Window

Credit card validation not working

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Web Design Discussion
Forum Description: Discussion on web design and development subjects.
URL: https://forums.webwiz.net/forum_posts.asp?TID=12816
Printed Date: 29 March 2026 at 10:14am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Credit card validation not working
Posted By: simflex
Subject: Credit card validation not working
Date Posted: 03 December 2004 at 10:29am
Dear gurus,
I have a credit card script that should check for the card type and determine that a user entered correct value for the card.
 
For instance, Master card, Visa and Discover have 16 digits.
 
I am trying to ensure that if a user enters less than 16 digitis, there should be an error flag.
 
Similarly, if a user enters more than the required digits, he or she should be flagged.
 
So far, as long as the card is up to 15 digits, it is processed.
 
Please take a look and advise.
 
Thanks in advance.
 

<SCRIPT LANGUAGE=javascript>
<!--
// Client script validates form field entries for credit card
function validate(theForm)
 {
  if(document.cform.IsHidden.value=="false")
   {
    if (theForm.cardname.value == "" || theForm.cardname.value.length < 2)
     {
      alert("Please fill in the name found on your credit card.");
      theForm.cardname.focus() ;
      return false;
     }
     if ((theForm.paymentm = "Visa" || theForm.paymentm = "Dis" || theForm.paymentm = "MC" ) AND (theForm.cardno.value == "" || theForm.cardno.value.length < 16 || theForm.cardno.value == "0000-0000-0000-0000")
     {
      alert("Please fill in the card number in this format: 0000-0000-0000-0000.");
      theForm.cardno.focus();
      return false;
     }
     return true;
   }
 }
function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
</script>
 
Then I will invoke it here:
 

   <td width="350">
    <select name="paymentm" onChange="isValidCreditCard(this)">
     <option value="Disc">Discover</option>
     <option selected value="Visa">Visa</option>
     <option value="AmEx">American Express</option>
     <option value="MC">Mastercard</option>
     <option value="Diners">Diner's Club</option>
    </select>
   </td>




Replies:
Posted By: dj air
Date Posted: 03 December 2004 at 11:18am
you have
theForm.cardno.value.length < 16

you can use theForm.cardno.value.length <> 16

that is if its above or below 16


Posted By: dpyers
Date Posted: 03 December 2004 at 12:36pm
This is the validation criteria I use.
  1. Note that Visa cc length can be 13 or 16
  2. Diners prefixes are 300;301;302;303;304;305;36; and 38. You're better off to explicitly check for the 30x values as 306, 307, 308, 309 will not be valid.
select case ctype
    case "VISA"
      cclength="13;16"
      ccprefix="4"
    case "Mastercard/Eurocard"
      cclength="16"
      ccprefix="51;52;53;54;55"
    case "American Express"
      cclength="15"
      ccprefix="34;37"
    case "Diners Club/Carte Blanche"
      cclength="14"
      ccprefix="300;301;302;303;304;305;36;38"
    case "Discover"
      cclength="16"
      ccprefix="6011"
    case "enRoute"
      cclength="15"
      ccprefix="2014;2149"
    case "JCB"
      cclength="15;16"
      ccprefix="3;2131;1800"
 


-------------

Lead me not into temptation... I know the short cut, follow me.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net