Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
987 มุมมอง

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.

อวตาร
ละทิ้ง

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

คำตอบที่ดีที่สุด

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

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ส.ค. 25
450
Change position chatter แก้ไขแล้ว
1
ส.ค. 25
430
4
ก.ค. 25
1726
2
ก.ค. 25
1001
1
ก.ค. 25
2142