Here is an old javascript from Netscape, I believe someone
posted to this list a while ago. It does one validity test,
doesn't do anything regarding comparing the CC number and CC
type (Visa, MC, Amex), etc.



Hope this helps:

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

<SCRIPT LANGUAGE="JavaScript">
<!--

// change it to false if you do not want
//the credit card number to be encrypted
var encrypt_it = true;


/* ==================================================================
   THIS FUNCTION IS TAKEN DIRECTLY FROM NETSCAPE FROM:
   http://developer.netscape.com/library/examples/...
                    .../javascript/formval/FormChek.js
   which is a bunch of functions to validate forms
    
   FUNCTION: isCreditCard(st)
   INPUT:    st - a string representing a credit card number
   RETURNS:  true, if the credit card number passes the Luhn Mod-10 test
                 false, otherwise
   ================================================================== */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}

function getCCNum(default_val) {
 msg = 'Please enter your credit card number here. '
  + 'It will be '
  + ((encrypt_it) ? "encrypted and then " : "")
  + 'put into the credit card field of the form '
  + 'after it is validated.';
 return prompt(msg,default_val);
}

// takes in a credit card number, adds one to each digit
// (9 becomes 0), and then returns the encrypted credit
// card number with an 'e' tacked on to the end to signal
// the number has been encrypted
function encrypt(val) {
 val = "" + val;
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1) {
   character = parseInt(character);
   character = (character+1)%10;
  }
  result += character;
 }
 if (result != "")
  result += "e";
 return result;
}

function unencrypt(val) {
 val = "" + val;
 for (n=0;n<9;n++)
  val = encrypt(val);
 return (val.substring(0,val.indexOf('e')));
}

function strip(val) {
 val = "" + val;
 if (!val)
  return "";
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1)
   result += character;
 }
 return result;
}

var last_entry = "";
function doCCStuff(form_element) {
 if (blur_reset) {
  last_entry = form_element.value;
  if (last_entry && last_entry.indexOf('e') != -1)
   last_entry = unencrypt(last_entry);
  entry = getCCNum(last_entry);
  stripped_entry = strip(entry);
  while (entry && (!isCreditCard(stripped_entry))) {
   alert('The credit card number you entered could not be '
    + 'validated. Please check the number and try again.');
   last_entry = entry;
   entry = getCCNum(last_entry);
   stripped_entry = strip(entry);
  }
  if (entry) {
   if (encrypt_it)
    form_element.value = encrypt(entry);
   else
    form_element.value = entry;
  }
  blur_form(form_element);
 }
 return false;
}
var blur_reset = true;
function blur_form(form_element) {
 form_element.blur();
 blur_reset = false;
 setTimeout("blur_reset=true",2000);
}

// -->
</SCRIPT>

<!-- Then add the onFocus event handler to your form element
     for credit card number entry. This is the onFocus event
     handler that you want to use:
     
     onFocus="return doCCStuff(this)"
     
     The form element should be a regular <input type="text">
     form element. It will look something like this:
     <input type="text" name="cc_num"
      onFocus="return doCCStuff(this)">
-->
     

Thursday, October 10, 2002, 11:48:17 AM, Jacob Stevens wrote:

> Hello Everyone,

> If possible could someone please send me a link to or an example of the
> equations used to verify the validity of different credit card numbers.

> Thanks,

> Jake Stevens

> ________________________________________________________________________
> TO UNSUBSCRIBE: send a plain text/US ASCII email to [EMAIL PROTECTED]
>                 with unsubscribe witango-talk in the message body


________________________________________________________________________
TO UNSUBSCRIBE: send a plain text/US ASCII email to [EMAIL PROTECTED]
                with unsubscribe witango-talk in the message body

Reply via email to