gpt4 book ai didi

javascript - 输入表单中的禁止字符?

转载 作者:行者123 更新时间:2023-12-02 19:41:57 24 4
gpt4 key购买 nike

这是我当前的代码,当用户键入时,它会从输入中删除除 $、逗号和点之外的所有非数字字符:

<input type="text" id="price" name="price" onkeyup="updatePrice(this.value)">

function updatePrice(p) {
document.getElementById("price").value = p.replace(/[^0-9$.,]/g, '');
}

问题在于,它会在键入字符后删除字符,因此如果您键入 A,您会在消失之前看到它几分之一秒。 Keydown 不好,因为它在输入实际更改之前运行脚本。

如何才能完全防止这些禁止字符出现在输入中?

最佳答案

  • 当输入失去焦点时,使用 onblur 执行验证 - 用户在输入过程中不必意识到这一点。
  • 用户根本不必意识到这一点 - 您可以在提交时执行验证。

关于javascript - 输入表单中的禁止字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10292521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com