1from django.core.validators import RegexValidator
2from django.db import models
3PHONE_NUMBER_REGEX = RegexValidator(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$', 'only valid email is required')
4class SomeClass(models.Model):
5 phone = models.CharField(max_length=14, validators=[PHONE_REGEX])
6
1# for outside model uses
2from django.core.validators import RegexValidator
3
4validate_alphanumeric = RegexValidator(r'^[a-zA-Z0-9]*$', 'Only alphanumeric characters are allowed.')
5validate_alphanumeric("foo") # ok, nothing happens
6validate_alphanumeric("++") # raises a ValidationError
7
8ValidationError: [u'Only alphanumeric characters are allowed.']