Credit Card Validation : Validation « Form Control « JavaScript DHTML






Credit Card Validation


/*
JavaScript Unleashed, Third Edition
by Richard Wagner and R. Allen Wyke 

ISBN: 067231763X
Publisher Sams CopyRight 2000

*/
<html>
<head>
<title>Credit Card Validation</title>
<script language="JavaScript">
<!--begin script
function isCreditCard(textObj) {
 /*
  *  This function validates a credit card entry.
  *  If the checksum is ok, the function returns true.
  */
   var ccNum;
   var odd = 1;
   var even = 2;
   var calcCard = 0;
   var calcs = 0;
   var ccNum2 = "";
   var aChar = '';
   var cc;
   var r;
   
   ccNum = textObj.value;
   for(var i = 0; i != ccNum.length; i++) {
      aChar = ccNum.substring(i,i+1);
      if(aChar == '-') {
         continue;
      }

      ccNum2 = ccNum2 + aChar;
   }
   
   cc = parseInt(ccNum2);
   if(cc == 0) {
      return false;
   }
   r = ccNum.length / 2;
   if(ccNum.length - (parseInt(r)*2) == 0) {
      odd = 2;
      even = 1;
   }
   
   for(var x = ccNum.length - 1; x > 0; x--) {
      r = x / 2;
      if(r < 1) {
         r++;
      }
      if(x - (parseInt(r) * 2) != 0) {
         calcs = (parseInt(ccNum.charAt(x - 1))) * odd;
      }
      else {
         calcs = (parseInt(ccNum.charAt(x - 1))) * even;
      }
      if(calcs >= 10) {
         calcs = calcs - 10 + 1;
      }
      calcCard = calcCard + calcs;
   }
   
   calcs = 10 - (calcCard % 10);
   if(calcs == 10) {
      calcs = 0;
   }
   
   if(calcs == (parseInt(ccNum.charAt(ccNum.length - 1)))) {
      return true;
   }
   else {
      return false;
   }
}

// end script-->
</script>
</head>
   
<BODY>
<h1>Credit Card Validation</h1>
<form name="form1">
<input type="text"
   size=16
   maxlength=16
   name="data">
<input type="button"
   name="CheckButton"
   value="Validate"
   onClick="document.form1.result.value = '' +
    isCreditCard(document.form1.data)">
<br>
Result <input type="text"
   size=16
   maxlength=16
   name="result">
</form>
</body>
</html>

           
       








Related examples in the same category

1.Form validation: Not Empty, Valid Radio, is Number, string length, EMail Address
2.Validate an input field with minimum and maximum values
3.Validate an field with a maximum number of characters
4.Phone Number Validation
5.Creating Reusable Validation Code
6.Money Format
7.Validating User Input
8.Validate email address
9.Validate a number
10.TextField input length validator
11.Password field validator
12.TextField validator: email, IP address and URL
13.Must be at least 3 characters and not more than 8
14.Only characters are allowed
15.Can be empty
16.Must be a valid email address
17.ComboBox(list box): Must be selected one
18.InputBox: value must be between 10 and 90
19.Not stop when the first failed validation is encountered
20.A CSS is used to highlight the fields which failed to validate
21.Need to select a file
22.A simple form with two passwords that must have the same value
23.Shows the usage of callback functions for checking a field
24.Javascript validation framework
25.Form validator library