惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - ฅ˙-˙ฅ

用display实现效果:上面根据内容自适应高度;下面撑满所有,并且超出时候显示滚动条 react更改多层对象变量的方法 react umi model使用注意事项 拖动改变顺序 博文阅读密码验证 - 博客园 react函数式组件:父组件调用子组件方法 react form表单中自定义组件的数据双向绑定实现 vue3+vant h5: Rem 移动端布局适配之postcss-pxtorem和lib-flexible 项目记录文档 前端mock方案:使用json-server mac安装使用nginx 博文阅读密码验证 - 博客园 umi修改antd主题颜色(通过less文件修改) 部署一个vue项目到阿里云服务器 vue项目通过点击按钮实现复制图片(非图片url和base64),可发送到聊天框 vue重新刷新当前路由(非浏览器强刷,不会出现闪屏) 使用Vue.extend实现iview Upload在单文件上传时,拖拽多个文件给出错误提示 uni-app怎么使用路由守卫,并且路由配置和pages.json中只写一套 uni-app小程序iPhone X适配底部栏黑横线 使用css自定义变量实现实现主题切换功能
antd input控制只能输入数字并进行格式化显示(antd 3版本)
ฅ˙-˙ฅ · 2022-04-21 · via 博客园 - ฅ˙-˙ฅ

需求背景

登录输入手机号码,进行验证;需求要求输入时只能输入手机号码,并在第三位和第七位进行空格隔开显示;

效果


思路

antd form 中使用getValueFromEvent能够对输入值进行劫持改造,返回的数据会更新输入框的值;

代码

  1. 组件jsx
<Form.Item>
          {getFieldDecorator("phoneNo", {
            rules: [
              {
                required: true,
                message: "请输入您的手机号",
              },
              { validator: checkPhone },
            ],
            initialValue: "",
            getValueFromEvent: phoneNoFormat,
          })(<Input placeholder={"请输入手机号"} />)}
</Form.Item>
  1. 劫持方法phoneNoFormat
  // 只允许输入数字并用空格隔开电话号码
  const phoneNoFormat = (e) => {
    const val = e.target.value; // 旧值
    let newVal = val.substring(0, 13).replace(/[^\d]/g, ""); // 提取中字符串中的数字(只数字)
    // 检测到第4位数字和第8位数字时,在第3位和第7位加入空格
    // (注意:如果检测到第3位数字和第7位数字时添加空格(判断条件为>6和>2),删除时会导致删除到空格时无法继续删除,可自行尝试)
    if (newVal.length > 7) {
      newVal = newVal.replace(/^(.{3})(.{4})(.*)$/, "$1 $2 $3");
    } else if (newVal.length > 3) {
      newVal = newVal.replace(/^(.{3})(.*)$/, "$1 $2");
    }
    // 返回格式化之后的值
    return newVal;
  };
  1. 验证方法(验证时需要去重空格)
  const checkPhone = (rule, value, callback) => {
    var reg = "^1[0-9]{10}$"; //手机号码验证regEx:第一位数字必须是1,11位数字
    var re = new RegExp(reg);
    // 去掉空格
    const trueVal = value.replace(/\s*/g, "");
    if (!trueVal) {
      callback("请输入您的手机号");
      return;
    }
    if (!re.test(trueVal)) {
      callback("请输入正确的电话号码");
      return;
    }
    callback();
  };