Home > other >  Python Validator function for phone and email
Python Validator function for phone and email

Time:01-29

I have a website to validate spreadsheet headers and rows when a spreadsheet is uploaded to the site,here i have some validation function where i will have to validate the rows according the attributes of the field in my case i have model like below:

models.py

from phone_field.models import PhoneField

class Address(BaseModel):
phone = PhoneField(max_length=255, blank=True)
address_1 = models.CharField(max_length=255)
address_2 = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=255, blank=True)
email = models.EmailField(unique=True, blank=True, null=True)

I need to write the custom validators for phone and email while uploading the spreadsheet.

def phone_validator(value):
      code:
def email_validator(value):
      code




    

CodePudding user response:

You can see the Django document about the validators

https://docs.djangoproject.com/en/4.0/ref/validators/

also, you can use this one

https://www.programcreek.com/python/example/63699/django.core.validators.validate_email

CodePudding user response:

def validate_email(value):
    query = model.objects.all().values('field__email')
    try:
        if value not in query:
            return True
    except ValidationError:
        return False
  •  Tags:  
  • Related