JavaScript Tutorial/Regular Expressions/Credit Card

Материал из Web эксперт
Версия от 08:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Matching a 16 digit credit card number separated by a single space

<html>
<head>
<title>Matching a 16 digit credit card number separated by a single space</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingCard(str){
   var myRegExp =  /[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}/;
   return myRegExp.test(str)
}
function TestGuess(){
alert(IsMatchingCard("1234 1234 1234 1234"));
}
// -->
</script>
</head>
<body>
<h3>This page allows you to enter and check a 16 digit credit card number</h3>
<form>
<button type="Button" onclick="TestGuess()">
Click here to enter card number</button>
</form>
</body>
</html>


Matching a 16 digit credit card number with variable number of space characters

<html>
<head>
<title>Matching a 16 digit credit card number
 with variable number of space characters</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingCard(str){
    var myRegExp =  /[0-9]{4} {0,1}[0-9]{4} {0,1}[0-9]{4} {0,1}[0-9]{4}/;
    return myRegExp.test(str)
}
function TestGuess(){
alert(IsMatchingCard("1234 1234 1234 1234"));
}
// -->
</script>
</head>
<body>
<h3>This page allows you to enter and check a 16 digit credit card number</h3>
<form>
<button type="Button" onclick="TestGuess()">Click here to enter card
number</button>
</form>
</body>
</html>


Validate MasterCard

<html>
    <head>
        <title>MasterCard Example</title>
        <script type="text/javascript">
                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 isValidMasterCard(sText) {
                    var reMasterCard = /^(5[1-5]\d{2})[\s\-]?(\d{4})[\s\-]?(\d{4})[\s\-]?(\d{4})$/;
                    if (reMasterCard.test(sText)) {
                        var sCardNum = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
                        return luhnCheckSum(sCardNum);
                    } else {
                        return false;
                    }
                }
                function validate() {
                    var oInput1 = document.getElementById("txt1");
                    if (isValidMasterCard(oInput1.value)) {
                        alert("Valid");
                    } else {
                        alert("Invalid!");
                    }
                }
        </script>
    </head>
    <body>
        <P>MasterCard Number: <input type="text" id="txt1" /><br />
        <input type="button" value="Validate" onclick="validate()" /></p>
    </body>
</html>
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


Validate Visa number

<HTML>
    <head>
        <title>Visa Example</title>
        <script type="text/javascript">
                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!");
                    }
                }
        </script>
    </head>
    <body>
        <P>Visa Number: <input type="text" id="txt1" /><br />
        <input type="button" value="Validate" onclick="validate()" /></p>
    </body>
</html>
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