You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何控制Node List中的输入值?限制仅输入字母数字(无正则)

问题:限制文本输入框仅允许字母和数字(不使用正则表达式)

我获取了一个包含文本输入框的NodeList,想要强制用户只能输入字母和数字字符,而且不能使用正则表达式。我尝试用charCodeAt函数实现,但没有效果,以下是我的初始代码:

function verif(){
var nodes = document.querySelectorAll("input[type=text]");
for (var i=0; i<nodes.length; i++)
    var value=nodes[i]
    if (value.charCodeAt(i)<48 ||value.charCodeAt(i)>57  ){
        alert("Alphabetic and numeric values only")
        Return false;
    }

}

更新:在@blex的帮助和我自己的补充修改后,代码已经可以正常运行,以下是最终代码:

function verif(){
var nodes = document.querySelectorAll("input[type=text]");
for (var i=0; i<nodes.length; i++)
    var value=nodes[i].value
    for(var j =0;j<value.length;j++){
        if ((value.charCodeAt(j)<48 ||value.charCodeAt(j)>57) && ("A">=value[j].toLowerCase() && "Z">=value[j].toLowerCase())  ){
            console.log("Numeric and alphabetic values")
    }
}}

内容的提问来源于stack exchange,提问作者Mahdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 15:15:38