You need to enable JavaScript to run this app. ODOO 15联系人表单:手机号规则与默认区号设置问题
优惠活动
大模型
产品
解决方案
定价
更多

ODOO 15联系人表单:手机号规则与默认区号设置问题

Odoo 15 联系人表单手机号字段问题修复

问题1:区号输入框默认值+91不生效

问题根源

原代码中t-att-value="country_code or +91"不符合Odoo模板语法规则:字符串常量必须用单引号包裹,+91未加引号会被模板引擎当作无效表达式处理,导致默认值无法生效。

解决方法

将默认值字符串用单引号括起来,修改为:t-att-value="country_code or '+91'",确保模板能正确识别默认值。

问题2:手机号输入框允许0开头,未限制首位为9

问题根源

  1. 正则表达式pattern="^[0-9]{10}$"仅限制长度为10位,但未约束首位必须为9;
  2. 存在拼写错误:t-att-value="mobile_numer"中的mobile_numer应为mobile_number,会导致后端存储的手机号无法正确渲染到输入框中。

解决方法

  1. 修正正则表达式为pattern="^9[0-9]{9}$",强制要求首位为9,后续9位为数字,确保符合10位手机号格式要求;
  2. 修正拼写错误为t-att-value="mobile_number"。

修正后的完整代码

<div class="row mb-3">
    <label for="mobile" class="col-sm-4 col-form-label col-form-label-sm">Mobile Number
    </label>
    <div class="col-sm-8 mobile">
        <input type="text" class="form-control form-control-sm custom_class"
               name="country_code" id="country_code" maxlength="4" size="4" 
               t-att-value="country_code or '+91'" autocomplete="off"/>
        <input type="tel" minlength="10" maxlength="10" class="form-control form-control-sm custom_class" 
               id="mobile_number" autocomplete="off" name="mobile_number" 
               t-att-value="mobile_number" pattern="^9[0-9]{9}$"/>
    </div>
</div>

额外说明

  • 区号默认值逻辑:当country_code字段为空(False或空字符串)时,自动填充+91;若已有值则显示存储的区号;
  • 手机号验证:结合pattern、minlength和maxlength,浏览器会在用户提交表单时自动验证格式,不符合要求会阻止提交并提示错误。

内容的提问来源于stack exchange,提问作者Sidharth Panda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 16:42:47