gpt4 book ai didi

javascript - <input type =“text” maxlength =“4” > 不应将逗号和点计入 maxlength

转载 作者:行者123 更新时间:2023-11-28 17:57:36 26 4
gpt4 key购买 nike

我的输入字段包含数字和特殊字符,例如逗号和点在计算字段的 maxLength 时,我想跳过特殊字符。

我不想限制特殊字符。

预期输出应为:- 1,234(总长度:- 4)

<form action="/action_page.php">
Username: <input type="text" maxlength="3" id="myId"/>
<input type="submit" value="Submit">
</form>

jsfiddle链接here

最佳答案

尝试添加此 JavaScript:

 window.onload = function() {

var textInput = document.getElementById("myId");

textInput.oninput = function() {
var temp;
temp = this.value.replace(/[^\w\s]/gi,'');
if (temp.length > 3) {
alert("Invalid"); // Or other stuff you want to do
}
};
};

请注意,此代码实时检查输入

关于javascript - &lt;input type =“text” maxlength =“4” > 不应将逗号和点计入 maxlength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44174575/

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