使用正則表達式驗證
import re
from osv import osv,fields
class crm_lead_inherit(osv.osv):
_name = _inherit = 'crm.lead'
def create(self,cr,uid,vals,context=None):
if 'mobile' in vals and vals['mobile']:
if re.match("^[0-9]*$", vals['mobile']) != None:
pass
else:
raise osv.except_osv(_('Invalid Mobile No'),_('Please enter a valid Phone Number'))
return super(crm_lead_inherit, self).create(cr, uid,vals, context=context)
def write(self,cr,uid,ids,vals,context=None):
if 'mobile' in vals and vals['mobile']:
if re.match("^[0-9]*$", vals['mobile']) != None:
pass
else:
raise osv.except_osv(_('Invalid Mobile No'),_('Please enter a valid Phone Number'))
return super(crm_lead_inherit, self).write(cr, uid,ids,vals, context=context)
crm_lead_inherit()
這是正確的方法,welldone兄 –
@Senthilnathang:我是Openerp新手請問我該在哪裏粘貼此代碼 – nitesh
@Senthilnathang我得到這個錯誤「if re.match(」^ [ 0-9] * $「,vals ['mobile'])!=無: NameError:全局名稱're'未定義' – nitesh