39 lines
1.9 KiB
Python
39 lines
1.9 KiB
Python
from wtforms import StringField
|
|
from wtforms.validators import Regexp, ValidationError
|
|
|
|
class CreditCardField(StringField):
|
|
def __init__(self, label=None, validators=None, **kwargs):
|
|
super(CreditCardField, self).__init__(label, validators, **kwargs)
|
|
|
|
def pre_validate(self, form):
|
|
credit_card_fields = [form.credit_card_number.data, form.credit_card_expiry.data, form.credit_card_cvv.data]
|
|
if any(credit_card_fields) and not all(credit_card_fields):
|
|
raise ValidationError('Please fill in all credit card fields or leave them all empty.')
|
|
|
|
def validate_credit_card(self, form, field):
|
|
credit_card_fields = [form.credit_card_number.data, form.credit_card_expiry.data, form.credit_card_cvv.data]
|
|
if all(credit_card_fields):
|
|
print('It thinks not all of them are empty')
|
|
if not self.is_valid_credit_card(form.credit_card_number.data):
|
|
raise ValidationError('Invalid credit card number.')
|
|
if not self.is_valid_credit_card_expiry(form.credit_card_expiry.data):
|
|
raise ValidationError('Invalid credit card expiry date.')
|
|
if not self.is_valid_credit_card_cvv(form.credit_card_cvv.data):
|
|
raise ValidationError('Invalid credit card CVV.')
|
|
|
|
def is_valid_credit_card(self, card_number):
|
|
# Implement your credit card number validation logic here
|
|
# For example, you can use the Luhn algorithm
|
|
# Return True if the card number is valid, False otherwise
|
|
return True
|
|
|
|
def is_valid_credit_card_expiry(self, expiry_date):
|
|
# Implement your credit card expiry date validation logic here
|
|
# Return True if the expiry date is valid, False otherwise
|
|
return True
|
|
|
|
def is_valid_credit_card_cvv(self, cvv):
|
|
# Implement your credit card CVV validation logic here
|
|
# Return True if the CVV is valid, False otherwise
|
|
return True
|