Answers for "regex validator in django"

1

using regex validators in django models

from django.core.validators import RegexValidator
from django.db import models
PHONE_NUMBER_REGEX = RegexValidator(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-s./0-9]*$', 'only valid email is required')
class SomeClass(models.Model):
  phone =  models.CharField(max_length=14, validators=[PHONE_REGEX])
Posted by: Guest on September-18-2020
1

django regexvalidator example

# for outside model uses
from django.core.validators import RegexValidator

validate_alphanumeric = RegexValidator(r'^[a-zA-Z0-9]*$', 'Only alphanumeric characters are allowed.')
validate_alphanumeric("foo") # ok, nothing happens
validate_alphanumeric("++") # raises a ValidationError

ValidationError: [u'Only alphanumeric characters are allowed.']
Posted by: Guest on June-17-2021

Python Answers by Framework

Browse Popular Code Answers by Language