YaakovHatam
YaakovHatam

Reputation: 2344

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;
}

Upvotes: 0

Views: 696

Answers (2)

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

I am unable to find a formal definition of JScript syntax, but if it resembles JavaScript enough, then the first thing to do is to rename 9digitcard_ClientValidate into a name not starting with digits.

Upvotes: 1

Seb
Seb

Reputation: 85

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

Upvotes: 1

Related Questions