美文网首页
控制input输入数字非中文

控制input输入数字非中文

作者: sssgoEasy | 来源:发表于2020-01-03 16:16 被阅读0次

事件:1. onKeypress 2. onkeyup

代码:

        <input
            class="mobile-input"
            v-bind="$attrs"
            ref="input"
            :value="currentValue"
            @input="handleInput"
            @change="handleChange"
            v-focus="focusState"
            @click.stop="handleClick"
            maxlength="11"
            type="tel"
            onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
            onkeyup="value=value.replace(/[\u4e00-\u9fa5]/ig,'')"
            :placeholder="placeholder"
        />

方法:

  • 采用动态正则验证处理,以此拓展各种类型控制
  1. 数字:onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"

  2. 非中文:onkeyup="value=value.replace(/[\u4e00-\u9fa5]/ig,'')"

相关文章

网友评论

      本文标题:控制input输入数字非中文

      本文链接:https://www.haomeiwen.com/subject/ioozoctx.html