Skip to content

Instantly share code, notes, and snippets.

@wjdp
Last active August 29, 2015 14:17
Show Gist options
  • Save wjdp/af8569c20dd8462d0757 to your computer and use it in GitHub Desktop.
Save wjdp/af8569c20dd8462d0757 to your computer and use it in GitHub Desktop.
Django quick and dirty phone validation
from django.core.validators import validate_integer
from django.core.exceptions import ValidationError
MIN_PHONE_LENGTH = 11
MAX_PHONE_LENGTH = 15
def validate_phone(input):
"""Validate a phone number"""
no_symbols = re.sub(r'[^\w]', ' ', input)
no_symbols_or_whitespace = no_symbols.replace(' ', '')
validate_integer(no_symbols_or_whitespace)
length = len(no_symbols_or_whitespace)
if length < MIN_PHONE_LENGTH or length > MAX_PHONE_LENGTH:
raise ValidationError('Invalid length of phone')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment