Skip to Content
Menu
This question has been flagged
2 Replies
2735 Views

Hello,

I want to add validation for email fields and phone numbers, if the user enter text without @ validation error raise.



thank you in advance

Avatar
Discard
Best Answer

Hi,

You can do the validation according to the below code:

import re
from odoo.exceptions import ValidationError

@api.model
def create(self, vals):
    if vals.get('email'):
        match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', vals.get('email'))
        if match == None:
            raise ValidationError('Not a valid E-mail ID')

    if vals.get('number'):
        match = re.match('\\+{0,1}[0-9]{10,12}', vals.get('number'))
        if match == None:
            raise ValidationError('Invalid Phone Number')
    return super(MultiContacts, self).create(vals)

Regards

Avatar
Discard
Best Answer

Hello Jenam

you can implement constrains for your model as  i have done in res.partner model.

from odoo import api
from odoo.exceptions import UserError

class ResPartner(models.Model):
_inherit = "res.partner"

@api.constrains('email')
def _check_email_number(self):
for rec in self:
if rec.email and '@' not in rec.email:
raise UserError(_('Please Enter Correct Email'))

Thanks & Regards,



CandidRoot Solutions Pvt. Ltd.

Mobile: (+91) 8849036209
Email: info@candidroot.com
Skype: live:candidroot
Web: https://www.candidroot.com
Address: 1229-1230, Iconic Shyamal, Near Shyamal Cross Road, Ahmedabad, Gujarat



Avatar
Discard
Related Posts Replies Views Activity
2
Dec 23
11993
0
Oct 23
33
3
Oct 23
787
1
Oct 23
569
1
Aug 23
978