1. Zeit Code
  2. Untitled project
  3. django-form-extensions
Issue #1 new

CreditCardField Validation Incorrrect

patgmiller
created an issue

Using Django 1.4, the CreditCardField doesn't respect the default required=True argument, also it throws a fatal error when casting to an int if any non-numeric character is entered. Here is a suggestion for changing the clean method.

The CreditCardField doesn't respect the default required=True field attribute.

    def clean(self, value):
        value = value.replace(' ', '').replace('-', '')
        value = super(CreditCardField, self).clean(value)

        try:
            num = [int(digit) for digit in str(value)]
        except Exception, ex:
            raise ValidationError('Enter a valid credit card number.')

        valid = not sum(num[::-2] + [sum(divmod(d * 2, 10)) for d in num[-2::-2]]) % 10
        if not valid:
            raise ValidationError('Enter a valid credit card number.')
        return value