Overview | File | Class | Tree | Index | Help
| Package /org/apache/commons/validator/javascript
PREV NEXT | FRAMES NO FRAMES

## validateCreditCard.js

#### Summary

No overview generated for 'validateCreditCard.js'

Method Summary
`static Object` | `jcv_isLuhnNum(argvalue)`
`static Object` | `jcv_luhnCheck(cardNumber)` Checks whether a given credit card number has a valid Luhn checksum.
`static Object` | `validateCreditCard(form)` Check to see if fields are a valid creditcard number based on Luhn checksum.

    /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *    http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
        /*$RCSfile: validateCreditCard.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
        /**
        * Check to see if fields are a valid creditcard number based on Luhn checksum.
        * Fields are not checked if they are disabled.
        * @param form The form validation is taking place on.
        */
        function validateCreditCard(form) {
            var bValid = true;
            var focusField = null;
            var i = 0;
            var fields = new Array();

            var oCreditCard = eval('new ' + jcv_retrieveFormName(form) +  '_creditCard()');

            for (var x in oCreditCard) {
                if (!jcv_verifyArrayElement(x, oCreditCard[x])) {
                    continue;
                }
                var field = form[oCreditCard[x][0]];
                if (!jcv_isFieldPresent(field)) {
                  continue;
                }
                if ((field.type == 'text' ||
                     field.type == 'textarea') &&
                    (field.value.length > 0)) {
                    if (!jcv_luhnCheck(field.value)) {
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oCreditCard[x][1];
                        bValid = false;
                    }
                }
            }
            if (fields.length > 0) {
                jcv_handleErrors(fields, focusField);
            }
            return bValid;
        }

        /**
         * Checks whether a given credit card number has a valid Luhn checksum.
         * This allows you to spot most randomly made-up or garbled credit card numbers immediately.
         * Reference: http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html
         */
        function jcv_luhnCheck(cardNumber) {
            if (jcv_isLuhnNum(cardNumber)) {
                var no_digit = cardNumber.length;
                var oddoeven = no_digit & 1;
                var sum = 0;
                for (var count = 0; count < no_digit; count++) {
                    var digit = parseInt(cardNumber.charAt(count));
                    if (!((count & 1) ^ oddoeven)) {
                        digit *= 2;
                        if (digit > 9) digit -= 9;
                    };
                    sum += digit;
                };
                if (sum == 0) return false;
                if (sum % 10 == 0) return true;
            };
            return false;
        }

        function jcv_isLuhnNum(argvalue) {
            argvalue = argvalue.toString();
            if (argvalue.length == 0) {
                return false;
            }
            for (var n = 0; n < argvalue.length; n++) {
                if ((argvalue.substring(n, n+1) < "0") ||
                    (argvalue.substring(n,n+1) > "9")) {
                    return false;
                }
            }
            return true;
        }

Overview | File | Class | Tree | Index | Help
| Package /org/apache/commons/validator/javascript
PREV NEXT | FRAMES NO FRAMES

Copyright ® 2000-2005 - Apache Software Foundation
Documentation generated by JSDoc on Tue Nov 28 22:30:05 2006