在hr模块中,hr.employee类具有以下字段,因此对于此字段,我想要的是有效的电话号码,只有10个数字,如果我输入的数字超过10个,则应显示输入有效数字
的消息{{1}}
答案 0 :(得分:2)
import re
def is_phone(self, cr, uid, ids, context=None):
record = self.browse(cr, uid, ids)
pattern ="^[0-9]{10}$"
for data in record:
if re.match(pattern, data.phone):
return True
else:
return False
return {}
_constraints = [(is_phone, 'Error: Invalid phone', ['phone']), ]
这样您就可以将电话号码与正则表达式匹配。
查看方
<fields name="phone" onchange="is_phone()">
答案 1 :(得分:1)
如果要使用onchange方法在字段中的无效字符上发出警告, 然后试试这个:
def onchange_mobile(self, cr, uid, ids, mobile, context=None):
res = {}
if not mobile:
return res
if not mobile.isdigit():
# raise osv.except_osv(_('Invalid phone'),_('Please enter a valid phone'))
res['warning'] = "Phone number %s is invalid, please use only digits!" % mobile
res['value']['mobile'] = False # just erase the value entered
return res
或者,您可以覆盖工作类的写入方法,如果字段“移动”,则会引发错误。不是数字...喜欢
def write(self, cr, uid, ids, vals, context=None):
if 'mobile' in vals.keys() and not vals['mobile'].isdigit():
raise osv.except_osv(_('Invalid phone'),_('Please enter a valid phone'))
return super(your_class, self).write(cr, uid, ids, vals, context=context)
希望有所帮助
答案 2 :(得分:0)
def on_change('number'):
Check here length of number greater than 10 or not if greater, then rais or validate error
view side
<Fields name="name" onchange="on_change('number')">
或
使用约束击球手
或 签入创建方法