Credit Card Number and Trailing Spaces

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
I noticed something pretty annoying that could confuse customers when entering their credit card (because it confused me). I copied and pasted a test credit card number into the Card Number text box during checkout, and I got an "Invalid Credit Card" message. I verified the number a couple times, and it was correct. It turns out that it was trailing spaces at the end of the card that was throwing the Credit Card Validator off, so I thought that I would modify it myself to Trim the trailing spaces off and allow credit card numbers with spaces at the end to validate. Below are the easy steps to implement the enhancement:

1) Just modify the CreditCardValidator.EvaluateIsValid method of your nopCommerce 1.8 cart. It is located in the Nop.Controls project under Payment > Validators > CreditCardValidator.cs. Notice, the .TrimEnd I added to remove trailing spaces.

        
/// <summary>
        /// Determines whether the value in the credit card control is valid
        /// </summary>
        /// <returns>true if the value in the input control is valid; otherwise, false.</returns>
        /// Contributer: Bruce Leggett
        protected override bool EvaluateIsValid()
        {
            string valueToValidate = base.GetControlValidationValue(base.ControlToValidate).TrimEnd();
            int indicator = 1;
            int firstNumToAdd = 0;
            int secondNumToAdd = 0;
            char[] ccArr = valueToValidate.ToCharArray();
            for (int i = ccArr.Length - 1; i >= 0; i--)
            {
                char ccNoAdd = ccArr[i];
                int ccAdd = 0;
                if (!int.TryParse(ccNoAdd.ToString(), out ccAdd))
                {
                    return false;
                }
                if (indicator == 1)
                {
                    firstNumToAdd += ccAdd;
                    indicator = 0;
                }
                else
                {
                    if ((ccAdd + ccAdd) >= 10)
                    {
                        int temporary = ccAdd + ccAdd;
                        string num1 = temporary.ToString().Substring(0, 1);
                        string num2 = temporary.ToString().Substring(1, 1);
                        secondNumToAdd += Convert.ToInt32(num1) + Convert.ToInt32(num2);
                    }
                    else
                    {
                        secondNumToAdd += ccAdd + ccAdd;
                    }
                    indicator = 1;
                }
            }
            return (((firstNumToAdd + secondNumToAdd) % 10) == 0);
        }
        #endregion
    }


2) Compile
3) Copy the Nop.Controls.dll in the bin folder to your websites bin folder and viola!

I know it isn't much, but it was bugging me!

Enjoy!
13 years ago
Thanks
13 years ago
You're welcome
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.