Credit Card Validation : Validation : Form Control : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP
JavaScript DHTML Home »  Form Control   » [  Validation  ]   
 



Credit Card Validation

Please note that some example is only working under IE or Firefox.


/*
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(r2!= 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 8Has Download File
14.  Only characters are allowedHas Download File
15.  Can be emptyHas Download File
16.  Must be a valid email addressHas Download File
17.  ComboBox(list box): Must be selected one Has Download File
18.  InputBox: value must be between 10 and 90Has Download File
19.  Not stop when the first failed validation is encounteredHas Download File
20.  A CSS is used to highlight the fields which failed to validateHas Download File
21.  Need to select a fileHas Download File
22.  A simple form with two passwords that must have the same valueHas Download File
23.  Shows the usage of callback functions for checking a fieldHas Download File
24.  Javascript validation frameworkHas Download File
25.  Form validator libraryHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.