OpenERP Email validation

浪尽此生 提交于 2019-12-02 06:25:16

You need to modify your create write and validate functions.I hope your validateemail method is correct.Whenever the re.match is None, then warning will be showed.

def  ValidateEmail(self, cr, uid, ids, email):
    if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) == None:
        raise osv.except_osv('Invalid Email', 'Please enter a valid email address')
    return True

def create(self, cr, uid,values,context=None):
    if 'email' in values:
        self.ValidateEmail(cr,uid,[],values['email'])
    res = super(latest_base,self).create(cr,uid,values,context=context)
    return res

def write(self, cr, uid, ids, values, context=None):
    if 'email' in values:
        self.ValidateEmail(cr,uid,ids,values['email'])
    res = super(latest_base, self).write(cr, uid, ids, values, context=context)
    return res 

Propably you can override the create and write method to raise a ValidateError error for wrong input. References here: https://doc.openerp.com/trunk/server/api_models/

class latest_base(osv.osv):

   def create(self, cr, uid, values, context=None):
        if not self.ValidateEmail(cr,uid,[],values['email']):
            raise ValidateError()

This is more easy

from validate_email import validate_email
is_valid = validate_email('email@is.valid')

EM = (r"[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$")

def emailvalidation(email):

if email:
    EMAIL_REGEX = re.compile(EM)
    if not EMAIL_REGEX.match(email):
        raise ValidationError(_('''This seems not to be valid email.
        Please enter email in correct format!'''))
    else:
        return True
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!