-1

I have a problem with using the Luhn algorithm to validate credit cards.

I'm using this c# code:

void ServerValidation(object source, ServerValidateEventArgs args)
    {
        // use a RequiredFieldValidator to check for an empty value
        if (CreditCardNumber.Text == string.Empty) args.IsValid = true;
        args.IsValid = IsCreditCardValid(this.CreditCardNumber.Text);
    }

   protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);            
        CreditcardValidation.ServerValidate += ServerValidation;
    }

    public bool IsCreditCardValid(string cardNumber)
    {
        const string allowed = "0123456789";
        int i;

        StringBuilder cleanNumber = new StringBuilder();
        for (i = 0; i < cardNumber.Length; i++)
        {
            if (allowed.IndexOf(cardNumber.Substring(i, 1)) >= 0)
                cleanNumber.Append(cardNumber.Substring(i, 1));
        }
        if (cleanNumber.Length < 13 || cleanNumber.Length > 16)
            return false;

        for (i = cleanNumber.Length + 1; i <= 16; i++)
            cleanNumber.Insert(0, "0");

        int multiplier, digit, sum, total = 0;
        string number = cleanNumber.ToString();

        for (i = 1; i <= 16; i++)
        {
            multiplier = 1 + (i % 2);
            digit = int.Parse(number.Substring(i - 1, 1));
            sum = digit * multiplier;
            if (sum > 9)
                sum -= 9;
            total += sum;
        }
        return (total % 10 == 0);

And this in aspx:

Creditcard number: <asp:TextBox ID="CreditCardNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="CreditCardNumberReq" ControlToValidate="CreditCardNumber" ErrorMessage="Please enter a Number" runat="server" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CreditcardValidation"  ControlToValidate="CreditCardNumber" onservervalidate="ServerValidation" ErrorMessage="Please enter a valid Creditcard Number" runat="server" Display="Dynamic"></asp:CustomValidator>

I'm always getting error, because I don't get the value from my .aspx to c# or something. This is the specific error I'm getting:

Description: Error compiling a resource required to service this request. Check the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'ASP.controls_midtermucc_ascx' does not contain a definition for 'Server Validation', and there was no extension method 'Server Validation' to be found that accepts a first argument of type 'ASP.controls_midtermucc_ascx' (are you missing a using directive or an assembly reference? )

What do I have to do?

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
user2157063
  • 49
  • 2
  • 10
  • 1
    What error are you getting? We can't help if you make us guess the error. – Simon Halsey Oct 13 '13 at 23:05
  • it says something like: `Description: Error compiling a resource required to service this request. Check the following specific error details and modify your source code appropriately.   Compiler Error Message: CS1061: 'ASP.controls_midtermucc_ascx' does not contain a definition for 'Server Validation', and there was no extension method 'Server Validation' to be found that accepts a first argument of type 'ASP.controls_midtermucc_ascx' (are you missing a using directive or an assembly reference? ).` – user2157063 Oct 13 '13 at 23:25

1 Answers1

1

You need to make your server method protected. It's currently private. The aspx page inherits from your code behind class. If the method is private, not protected or public, it's not visible to sub-classes.

So this: protected void ServerValidation(object source, ServerValidateEventArgs args)

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32