Created
January 8, 2019 11:11
-
-
Save pareksha/d56467660cb3a0a65789210451518bae to your computer and use it in GitHub Desktop.
Easy validation for contact number/mobile number
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
valid_contact = True | |
# Check if contact can be converted to integer (use regex match if chars like + are allowed) | |
# Although it is suggested to store coutry codes separately | |
try: | |
contact_number = int(data['contact_number']) | |
except ValueError: | |
valid_contact = False | |
# Change length according to needs, generally kept between 10 and 15, strictly 10 used here | |
if len(data['contact_number']) != 10 or not valid_contact: | |
return Response({"status": "error", "message": "Provided contact number is invalid. Only 10 digits are allowed."}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment