Last active
August 29, 2015 14:17
-
-
Save wjdp/af8569c20dd8462d0757 to your computer and use it in GitHub Desktop.
Django quick and dirty phone validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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