Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Credit card validation not working
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Topic ClosedCredit card validation not working

 Post Reply Post Reply
Author
simflex View Drop Down
Groupie
Groupie
Avatar

Joined: 10 November 2002
Location: United States
Status: Offline
Points: 63
Direct Link To This Post Topic: Credit card validation not working
    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>



Edited by simflex - 03 December 2004 at 10:37am
Back to Top
dj air View Drop Down
Senior Member
Senior Member
Avatar

Joined: 05 April 2002
Location: United Kingdom
Status: Offline
Points: 3627
Direct Link To This Post 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
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.