【问题标题】:Disable space in textbox (both typing spacebar and copy whitespace from other)禁用文本框中的空格(键入空格键和复制其他空格)
【发布时间】:2017-12-10 15:29:32
【问题描述】:

为了防止输入空格代码看起来像这样(Javascript)

function RestrictSpace() {
  if (event.keyCode == 32) {
    return false;
  }
}

但它不能防止粘贴空格 如何解决? 谢谢。

【问题讨论】:

    标签: javascript html network-security


    【解决方案1】:

    HTML

    <input type="text"onchange="myFunction(event)">
    

    JS

     function myFunction(event) {
        console.log(event.target.value)
           event.target.value = event.target.value.replace(/\s/g, "");
        }
    

    【讨论】:

    • 打字时不会弄乱光标位置吗?
    猜你喜欢
    • 2015-10-07
    • 2017-05-22
    • 2012-03-23
    • 2011-06-24
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多