有人知道这是怎么回事吗?我不明白为什么getElementByTagName首先应该是一个函数,它已经在函数内部,并且由提交按钮调用。
<body>
<form novalidate>
First Name:<input id="fname" pattern="[a-zA-Z\W]" type="text" name="First Name">
Last Name:<input id="lname" pattern="[a-zA-Z\W]" type="text" name="Last Name">
Number:<input id="num" pattern="[0-9\D]" type="text" name="Number">
Email:<input id="email" pattern="/^[a-zA-Z0-9\D\W._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/" type="text" name="Email">
<button type="button" id="submit">Send</button>
</form>
</body>
<script>
document.getElementById('submit').addEventListener("click", function() {
var inputFields = document.getElementByTagName("input");
for (var i = 0; i < inputFields.length; i++) {
var theInput = inputFields[i].value;
var thePattern = new RegExp(inputFields[i].pattern);
var theTest = thePattern.test(theInput);
console.log(theTest);
if (theTest === false) {
console.log(inputFields[i].name + " is incorrect");
}
}
} )
</script>