Sakthi Priya
Sakthi Priya

Reputation: 347

Validating mobile number - Odoo

In my xml file, i have the below field under a tree structure.

<field name="phone"/>

I have the below group under the form structure

<group>
<field name="phone" onchange="validate_phone(phone)" placeholder="Enter your mobile number"/>
</group>

In my python file:


    @api.depends('phone')
    @api.onchange('phone')
    def validate_phone(self):
        if self.phone:
            match = re.match('^[0-9]\d{10}$', self.phone)
            if match == None:
               raise ValidationError('Invalid')

There will be no error in code but still it does not work. Could any one help me to get through this?

Upvotes: 0

Views: 1561

Answers (1)

Adam Strauss
Adam Strauss

Reputation: 1999

First of all there is no need to use onchange attribute in xml keep it simple like

<group>
    <field name="phone" placeholder="Enter your mobile number"/>
</group>

Second try this on just using onchange decorator

@api.onchange('phone')
def validate_phone(self):
    if self.phone:
        match = re.match('^[0-9]\d{10}$', self.phone)
        if not match:
           raise ValidationError('Invalid')

Upvotes: 2

Related Questions