gpt4 book ai didi

javascript - 未捕获的类型错误 : Object [object RegExp] has no method 'match'

转载 作者:行者123 更新时间:2023-11-28 01:30:45 25 4
gpt4 key购买 nike

基本上我在控制台上遇到了错误:

Uncaught TypeError: Object [object RegExp] has no method 'match'

这是触发错误的代码:

addMember = function addMember(e) {
console.log(e.target);
var s1 = new SchoolAdmission();
s1.name = document.getElementById('name').value;
s1.age = document.getElementById('age').value;
s1.department = document.getElementById('department').value;
s1.display(e.target.id);
};

validate = function validate(e) {

if (nameInput.value == "") {
(errorInput).innerHTML = "Please fill all columns";
validation = false;
}
else if (!nameInput.match(charFilter)) {
(errorInput).innerHTML = "Please enter only alphabets";
validation = false;
}

if (ageInput.value == "") {
(errorInput).innerHTML = "Please fill all columns";
validation = false;
}
else if (!ageInput.match(numberFilter)) {
(errorInput).innerHTML = "Please enter only numbers";
validation = false;
}

if (validation) {
addMember(e);
}
};

我已经对正在构建的应用程序进行了验证。我现在刚刚将其应用到“添加学生”按钮,稍后将其移至“添加教师”。

<button id="addStudent" class="btn btn-success" onclick="validate(event)">Add Student</button>

这是我的 fiddle :http://jsfiddle.net/LPu9x/6/

我的问题是:

1) 解决此错误。

2)我的验证方法正确吗?

最佳答案

nameInput 是 dom 对象,因此它没有 match 方法,您需要获取输入的值然后使用 .match

nameInput.value.match(charFilter)

关于javascript - 未捕获的类型错误 : Object [object RegExp] has no method 'match' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22140927/

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