/************************************************************** cc_validate.js Set of JavaScript functions used to validate credit card numbers *********************************************************** The code contained herein contstitutes Medical Office Online, Inc's copyright and is intended for use by its customers and potential customers only. User may not modify, remove, delete, augment, add to, publish, transmit, participate in the transfer or sale of, create derivative works from, or in any way exploit any of the code contained herein, in whole or in part. This Web site is offered to the user conditioned on acceptance by the user without modification of the terms, conditions, and notices contained herein. By accessing and using this Web site, the User is deemed to have agreed to all such terms, conditions, and notices. ***********************************************************/ /*********************************************************** isValidCreditCardNumber() This function checks the validity of a credit card number. It does this by getting the card number and type and running a series of checks Arguments: cardNum - String = the credit card number cardType - String = the credit card type (mc,vs,ax) ************************************************************/ function isValidCreditCardNumber(cardNum, cardType) { var isValid = false; //check that cardNum contains numbers only var re1 = /^[\d]+$/; isValid = re1.test(cardNum); //check that cardNum has the right number of digits //and right prefix for the given card type if(isValid) { var lengthIsValid = false; var prefixIsValid = false; var prefixRegExp; switch(cardType) { case "mc": lengthIsValid = (cardNum.length == 16); prefixRegExp = /^5[1-5]/; break; case "vs": lengthIsValid = ((cardNum.length == 16) || (cardNum.length == 13)); prefixRegExp = /^4/; break; case "ax": lengthIsValid = (cardNum.length == 15); prefixRegExp = /^3(4|7)/; break; case "ds": lengthIsValid = (cardNum.length == 16); prefixRegExp = /^6011/; break; default: prefixRegExp = /^$/; alert("Card type not found"); } prefixIsValid = prefixRegExp.test(cardNum); isValid = prefixIsValid && lengthIsValid; } //Use the Luhn formula to check the validity of the entered credit card number if(isValid) { var currDigitInt var total = 0; for (i = cardNum.length-1; i>=0; i--) { currDigitInt = parseInt(cardNum.charAt(i),10); total = total + currDigitInt; i--; if(i >= 0) { currDigitInt = parseInt(cardNum.charAt(i),10) * 2; if(currDigitInt > 9) total = total + (currDigitInt-10)+1 else total = total + currDigitInt } } isValid = (total % 10 == 0); } return isValid; }