【问题标题】:Prevent Pasting in Input Field防止在输入字段中粘贴
【发布时间】:2021-09-01 21:47:40
【问题描述】:

当我尝试使用 RegEX 验证值时,它只允许十进制数字。(Numbers + Perion(.))。 这是我的 JS 正则表达式:

let numbers = value.replace(/[^0-9]+(\.[0-9]+{0,5}+)?/g, "");

它给了我这个错误:

Invalid regular expression: /[^0-9]*(\.[0-9]+{0,5})?/: Nothing to repeat

我试图找出我错在哪里,但无法找到问题!

【问题讨论】:

    标签: javascript jquery regex


    【解决方案1】:

    请关注这里

    HTML代码:

    <input type="text" value="" id="fieldname" name="Fieldname" >
    

    Javascript:

    window.onload = () => { const myInput = document.getElementById('fieldname'); myInput.onpaste = e => e.preventDefault(); }
    

    【讨论】:

    • 感谢您的帮助,但我在 JS 端使用了 oninput 和 IsNan,使用 parseFlot 和 parseInt 进行验证,它正在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 2018-02-26
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多