I want to restrict the user to entering only 10 numbers in the phone field. Example: The user enters the number 0553448950 only and cannot add more, noting that this is converted to the international format +966 55 344 8950. What I care about is only preventing the user from adding more than 10 numbers.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Księgowość
- Zapasy
- PoS
- Project
- MRP
To pytanie dostało ostrzeżenie
Hi,
Try the following code.
class ResPartner(models.Model):
_inherit = 'res.partner'
@api.constrains('phone')
def _check_phone_length(self):
for rec in self:
if rec.phone:
digits = re.sub(r'\D', '', rec.phone) # Remove non-digit characters
if len(digits) != 10:
raise ValidationError("Phone number must contain exactly 10 digits.")
- Removes all non-digit characters (so +966 55 344 8950 becomes 966553448950)
- Checks if the last 10 digits (i.e., local number) are exactly 10 digits long
- If not, raises a clear ValidationError
Hope it helps
Podoba Ci się ta dyskusja? Dołącz do niej!
Stwórz konto dzisiaj, aby cieszyć się ekskluzywnymi funkcjami i wchodzić w interakcje z naszą wspaniałą społecznością!
Zarejestruj sięPowiązane posty | Odpowiedzi | Widoki | Czynność | |
---|---|---|---|---|
Change position chatter
Rozwiązane
|
|
1
sie 25
|
164 | |
|
4
lip 25
|
1497 | ||
|
2
lip 25
|
849 | ||
|
1
lip 25
|
1964 | ||
|
2
lip 25
|
232 |
We don't recommend this:
- the number of digits for a valid phone number varies by country
- sometimes it is useful to enter the extension
You would need to write code to do this, either via a module or via an Automation Rule that counts the characters and blocks the User from saving