我正在开发一个基本的HTML5 / JavaScript页面。我被困在获取行“var textInput = dataEntry.value;”从表单中提取数据。因为它现在就在我尝试输入一个字符时它将接受任何字符并且我收到消息“TypeError:无法读取未定义的属性'值'”任何人都可以向我解释为什么我会收到此错误吗?
我现在的代码如下:
< script type = "text/javascript" >
/* <![CDATA[ */
function validateLetter(dataEntry) {
try {
var textInput = dataEntry.value;
var replacedInput = /[^A-Za-z]/g;
if (replacedInput.test(textInput) == false)
throw "You can only enter letters into this field.";
} catch (textInputError) {
window.alert(textInputError)
return false;
} finally {
dataEntry.value = form.toLowerCase()
}
return true;
}
/* ]] */
< /script>
<form action="validateTheCharacters" enctype="application/x-www-form-urlencoded" name="dataEntry">
<p>Enter your mother's maiden name:
<input type="text" id="letter1" name="letter1" onkeypress="validateLetter()">
</p>
<p>Enter the city you were born in:
<input type="text" id="letter2" name="letter2" onkeypress="validateLetter()">
</p>
<p>Enter the street you grew up on:
<input type="text" id="letter3" name="letter3" onkeypress="validateLetter()">
</p>
<p>Enter your phone number:
<input type="text" id="number1" name="number1" onkeypress="validateNumber()">
</p>
<p>Enter the year you were born:
<input type="text" id="number2" name="number2" onkeypress="validateNumber()">
</p>
<p>Enter the number of siblings you have:
<input type="text" id="number3" name="number3" onkeypress="validateNumber()">
</p>
<p>
<button type="reset" value="Reset Form">Reset Form</button>
</p>
</form>
答案 0 :(得分:0)
您将元素作为参数。试试onkeypress =“validateLetter(this)”
答案 1 :(得分:0)
正如dfsq所说,当你调用validateLetter()时,你没有传递任何东西。您需要传入当前对象。所以试试这个:
onkeypress事件=“validateLetter(本);