0

I making django webapp. on that webapp user can register with there email or phone number sometime user enter mobile number with country code and sometime user not enter country code that time i need get user ip address and find the country code and send that number to twilio lookup API and verify before send sms code.

What is the best way to do this one

    def clean_email_or_phone(self):

    bad_domains = ['']
    email_or_phone = self.cleaned_data['email_or_phone']
    if "@" in email_or_phone:
        try:
            validate_email(email_or_phone)
            email_domain = self.cleaned_data['email_or_phone'].split('@')[1]
            if email_domain in bad_domains:
                raise forms.ValidationError(validators.FREE_EMAIL)
        except forms.ValidationError:
            raise forms.ValidationError(validators.FREE_EMAIL)

        if User.objects.filter(email=email_or_phone).exists():
            raise forms.ValidationError(validators.DUPLICATE_EMAIL)
    else:

    return email_or_phone

else part will mobile number verification

Thanks

1 Answers1

1

Twilio developer evangelist here.

I would recommend accepting phone numbers and emails in two different form fields so that you can know which one you're validating. That way you can also use a drop down menu / separate field to accept the country code.

Here's what that looks like on Twilio's sign up form and check out this stack overflow post for how to do that in Javascript. input phone number

For the Lookup portion, you can do the following in Python:

from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'ACxxxxxxx'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

phone_number = client.lookups.phone_numbers(email_or_phone).fetch(type="carrier")

print(phone_number.caller_name)
Kelley Robinson
  • 768
  • 1
  • 6
  • 10
  • Hello Kelley , Thank you for your suggestion. yes your correct if it come as two then i can easily do this but i need do that like how facebook, instagram and other site does that .it will good for user experience. – Lakshitha Kumara May 10 '18 at 04:36
  • Sounds good. Let me know if the lookup portion works for you! – Kelley Robinson May 11 '18 at 12:00
  • Just note that it costs 0.005 per requests to look up a phone number with type 'carrier'. https://www.twilio.com/lookup#pricing – Lewix Apr 01 '20 at 17:32