Regular Expressions JavaScript Tutorial

< HTML>
    
        Visa Example
        
                function luhnCheckSum(sCardNum) {
                    var iOddSum = 0;
                    var iEvenSum = 0;
                    var bIsOdd = true;
                    for (var i=sCardNum.length-1; i >= 0; i--) {
                        var iNum = parseInt(sCardNum.charAt(i));
                        if (bIsOdd) {
                            iOddSum += iNum;
                        } else {
                            iNum = iNum * 2;
                            if (iNum > 9) {
                                iNum = eval(iNum.toString().split("").join("+"));
                            }
                            iEvenSum += iNum;
                        }
                        bIsOdd = !bIsOdd;
                    }
                    return ((iEvenSum + iOddSum) % 10 == 0);
                }
                function isValidVisa(sText) {
                    var reVisa = /^(4\d{12}(?:\d{3})?)$/;
                    if (reVisa.test(sText)) {
                        return luhnCheckSum(RegExp.$1);
                    } else {
                        return false;
                    }
                }
                function validate() {
                    var oInput1 = document.getElementById("txt1");
                    if (isValidVisa(oInput1.value)) {
                        alert("Valid");
                    } else {
                        alert("Invalid!");
                    }
                }
        
    
    
        

Visa Number: 
        


    

Quote from:
Professional JavaScript for Web Developers
by Nicholas C. Zakas (Author)
# Paperback: 672 pages
# Publisher: Wrox (April 22, 2005)
# Language: English
# ISBN-10: 0764579088
# ISBN-13: 978-0764579080