You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
909 B
25 lines
909 B
import re
|
|
|
|
from django import forms
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
CREDIT_CARD_RE = r'^(?: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})$'
|
|
|
|
|
|
class CreditCardField(forms.CharField):
|
|
"""
|
|
Form field that validates credit card numbers.
|
|
"""
|
|
|
|
default_error_messages = {
|
|
'required': _(u'Номер карты обязателен.'),
|
|
'invalid': _(u'Неверный номер карты.'),
|
|
}
|
|
|
|
def clean(self, value):
|
|
value = value.replace(' ', '').replace('-', '')
|
|
if self.required and not value:
|
|
raise forms.utils.ValidationError(self.error_messages['required'])
|
|
if value and not re.match(CREDIT_CARD_RE, value):
|
|
raise forms.utils.ValidationError(self.error_messages['invalid'])
|
|
return value
|
|
|