Skip to content

Instantly share code, notes, and snippets.

@pareksha
Created January 8, 2019 11:11
Show Gist options
  • Save pareksha/d56467660cb3a0a65789210451518bae to your computer and use it in GitHub Desktop.
Save pareksha/d56467660cb3a0a65789210451518bae to your computer and use it in GitHub Desktop.
Easy validation for contact number/mobile number
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