Skip to Content
Menu
This question has been flagged
3 Replies
9716 Views

H all,,i want to add phone validation for a field,,it should not have any alphabets and also it can have different types of phone number say pakistan,india,us,uk etc,,can someone tell plz?

number formats  are:

03167470174
7513200000

and some US and UK  numbers etc


Avatar
Discard
Best Answer

For digits verification use this:

import re
if re.match("^[0-9]\d{10}$", '<Your Mobile Numner Input>') == None:
             raise ValidationError("Enter valid 10 digits Mobile number")

For Country Verification Use Python Package Phonenumbers. To install package do
pip install phonenumbers # change Pip Version accordingly
Then do constaraints in odoo for the number field like for example filed mobile.
@api.constrains(mobile):
def constrains_mobile(self):
...
About the Usage of Phonenumbers Country Validation you can check their documentation.
Here I can List some examples.
"""Python phone number parsing and formatting library

Examples of use:

>>> import phonenumbers
>>> from phonenumbers.util import prnt # equivalent to Py3k print()
>>> x = phonenumbers.parse("+442083661177", None)
>>> prnt(x)
Country Code: 44 National Number: 2083661177
>>> type(x)
<class 'phonenumbers.phonenumber.PhoneNumber'>
>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL))
'020 8366 1177'
>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
'+44 20 8366 1177'
>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164))
'+442083661177'
>>> y = phonenumbers.parse("020 8366 1177", "GB")
>>> prnt(y)
Country Code: 44 National Number: 2083661177
>>> x == y
True
>>>
>>> formatter = phonenumbers.AsYouTypeFormatter("US")
>>> prnt(formatter.input_digit("6"))
6
>>> prnt(formatter.input_digit("5"))
65
>>> prnt(formatter.input_digit("0"))
650
>>> prnt(formatter.input_digit("2"))
650-2
>>> prnt(formatter.input_digit("5"))
650-25
>>> prnt(formatter.input_digit("3"))
650-253
>>> prnt(formatter.input_digit("2"))
650-2532
>>> prnt(formatter.input_digit("2"))
(650) 253-22
>>> prnt(formatter.input_digit("2"))
(650) 253-222
>>> prnt(formatter.input_digit("2"))
(650) 253-2222
>>>
>>> text = "Call me at 510-748-8230 if it's before 9:30, or on 703-4800500 after 10am."
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
... prnt(match)
...
PhoneNumberMatch [11,23) 510-748-8230
PhoneNumberMatch [51,62) 703-4800500
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
... prnt(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164))
...
+15107488230
+17034800500
>>>
"""








Avatar
Discard
Best Answer

Hi,

Try this app which is used for the phone number validation https://apps.odoo.com/apps/modules/12.0/base_phone/

Avatar
Discard