简体   繁体   中英

credit card validator with jQuery

This code not working and i can't understand why:

this is special 9 digit card validator,

i have a textbox in the page "9digitCardTextBox" and calling with clientside validator on .aspx page.

the error: Runtime error in Microsoft JScript: Object required.

the validator:

<asp:CustomValidator runat="server" ID="CustomValidator16" EnableClientScript="true" 
Display="Dynamic" OnServerValidate="9digitcard_ServerValidate" CssClass="error" 
SetFocusOnError="true" 
ClientValidationFunction="9digitcard_ClientValidate">cc not good</asp:CustomValidator>

the clientside call:

function 9digitcard_ClientValidate(sender, e)
{        
    num = $("input[name*='9digitCardTextBox']").val();                
    sum = 0; mul = 1; l = num.length;
    for (i = 0; i < l; i++)
    {
        digit = num.substring(l-i-1,l-i);
        tproduct = parseInt(digit ,10)*mul;
        if (tproduct >= 10)
            sum += (tproduct % 10) + 1;
        else
            sum += tproduct;
        if (mul == 1)
            mul++;
        else
            mul–-;
    }
    if ((sum % 10) == 0)
        e.isValid = true;
    else
        e.isValid = false;
}

No need to use jquery for that, just a js match would be enouph:

var patt=/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/g;
var mastercard    = "5123625454565122";
var random        = "21236258456565122";
var isValid       = patt.test(mastercard);

if(isValid){
    document.write("mastercard : credit card nbr is valid.<br />"); 
}else{
    document.write("mastercard : credit card nbr is NOT valid!<br />" ); 
}

var isValid     = patt.test(random);
if(isValid){
    document.write("random : credit card nbr is valid.<br />"); 
}else{
    document.write("random : credit card nbr is NOT valid!<br />" ); 
}

see : http://jsfiddle.net/TbDR2/

edit : you can find other regexp for credit card number validation here : http://www.regular-expressions.info/creditcard.html

我找不到JScript语法的正式定义,但是如果它足够类似于JavaScript,那么要做的第一件事就是将9digitcard_ClientValidate重命名为一个不以digits开头的名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM