Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Credit card validation not working

    Hi All,

    This is my *first time* on this forum.
    I have been trying now, unsuccessfully to validate a credit js script that I have been working on for days.

    When I test it, it does nothing.

    Any one has any ideas, please?

    Here is the entire code, hope it is not too much.

    Code:
    <SCRIPT LANGUAGE=javascript>
    <!--
    // Client script validates form field entries for credit card
    function validate(theForm)
    {
    if(document.cform.IsHidden.value=="false")
    {
    if (theForm.cardname.value == "" || theForm.cardname.value.length < 2)
    {
    alert("Please fill in the name found on your credit card.");
    theForm.cardname.focus() ;
    return false;
    }
    if ((theForm.paymentm = "Visa" || theForm.paymentm = "Disc" || theForm.paymentm = "MC" ) && (theForm.cardno.value == "" || theForm.cardno.value == "0000-0000-0000-0000")
    {
    alert("Please fill in the card number in this format: 0000-0000-0000-0000.");
    theForm.cardno.focus();
    return false;
    }
    return true;
    }
    }
    function isValidCreditCard(type, ccnum) {
    
    validLength = true;
    if (type == "Visa") {
    if (ccnum.length != 19){validLength = false} // I ued 19, 16 digits plus 3 dashes
    // Visa: length 16, prefix 4, dashes optional.
    var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
    } if (type == "MC") {
    if (ccnum.length != 19){validLength = false}
    // Mastercard: length 16, prefix 51-55, dashes optional.
    var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
    } if (type == "Disc") {
    if (ccnum.length != 19){validLength = false}
    // Discover: length 16, prefix 6011, dashes optional.
    var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
    } if (type == "AmEx") {
    if (ccnum.length != 18){validLength = false}
    // American Express: length 15, prefix 34 or 37.
    var re = /^3[4,7]\d{13}$/;
    } if (type == "Diners") {
    if (ccnum.length != 17){validLength = false}
    // Diners: length 14, prefix 30, 36, or 38.
    var re = /^3[0,6,8]\d{12}$/;
    }
    if (!validLength) return false;
    if (!re.test(ccnum)) return false;
    // Checksum ("Mod 10")
    // Add even digits in even length strings or odd digits in odd length strings.
    var checksum = 0;
    for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
    checksum += parseInt(ccnum.charAt(i-1));
    }
    // Analyze odd digits in even length strings or even digits in odd length strings.
    for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
    var digit = parseInt(ccnum.charAt(i-1)) * 2;
    if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
    }
    if ((checksum % 10) == 0) return true; else return false;
    }
    </script>
    Then I invoke the script like this:

    Code:
    <td width="350">    <select name="paymentm" onChange="isValidCreditCard(this)">     <option value="Disc">Discover</option>     <option selected value="Visa">Visa</option>     <option value="AmEx">American Express</option>     <option value="MC">Mastercard</option>     <option value="Diners">Diner's Club</option>    </select>   </td>
    Many thanks in advance
    Last edited by samflex; 02-02-2005 at 05:29 PM.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    At best if you passed this.value when the function is called on your select onchange event handler you would still only be passing the type of card but your function is expecting two arguements... The second being the user entered account number ( ccnum )...

    .....Willy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •