[英]Simple Javascript text box verification error
I'm trying to check whether the length of characters typed into the text box is less than 6, and if it is, I want its background to be red. 我正在尝试检查在文本框中键入的字符的长度是否小于6,如果是,我希望其背景为红色。 Unfortunately, I can't seem to figure out where I'm going wrong with this simple problem. 不幸的是,我似乎无法弄清楚这个简单问题出了什么问题。
var textBox = getElementsByName('random'); function checkLength() { if (textBox.value.length < 6) { textBox.style.backgroundColor = "red"; } }
<input type="text" name="random" onfocus="checkLength();">
A few issues in your code: 您的代码中的几个问题:
<script>
code at the end, so that DOM is loaded and ready before you access elements in it. 您需要将<script>
代码放在末尾,以便在访问DOM中的元素之前已加载并准备好DOM。 getElementsByName('random')
needs to document.getElementsByName('random')
, which will actually return a list so you need to get first element from the list. getElementsByName('random')
需要document.getElementsByName('random')
,这实际上会返回一个列表,因此您需要从列表中获取第一个元素。 oninput
event. 同样从逻辑oninput
一旦输入的文本长度超过6,就需要删除红色背景,如果将函数附加到oninput
事件上会更好。 <input type="text" name="random" oninput="checkLength();"> <script type="text/javascript"> var textBox = document.getElementsByName('random')[0]; function checkLength() { if (textBox.value.length < 6) { textBox.style.backgroundColor = "red"; } else { textBox.style.backgroundColor = "white"; } } </script>
When the page first loads, the element with a name of random
doesn't exist. 首次加载页面时,名称为random
的元素不存在。
You will need to initialise your textBox
global after the page loads . 页面加载后,您将需要全局初始化textBox
。
You can do this by replacing 您可以通过替换
var textBox = document.getElementsByName("random")[0]
with 与
var textBox;
window.onload = function() {
textBox = document.getElementsByName("random")[0]
}
Try this 尝试这个
// add an id of "random" to your input
function checkLength() {
const textBox = document.getElementById("random")
if (textBox.value.length < 6) {
textBox.style.backgroundColor = "red";
} else {
textBox.style.backgroundColor = "white";
}
}
Working example: http://jsbin.com/caseqekusi/1/edit?html,js,output 工作示例: http : //jsbin.com/caseqekusi/1/edit?html,js,output
Note: If you want the box to be red right away, you'll have to modify it a bit, let me know if you have questions. 注意:如果您希望该框立即变为红色,则必须对其进行一些修改,如果您有任何疑问,请告诉我们。
I would suggest to use oninput
as well, so it updates as you type and marks the field as "valid" as soon as you have reached a certain length. 我建议也使用oninput
,这样,当您键入内容并达到一定长度后,它就会随着您的输入而更新并将其标记为“有效”。
You could also get rid of var textbox = ...
by using document.activeElement
. 您也可以使用document.activeElement
摆脱var textbox = ...
It makes your function reusable for other input fields. 它使您的功能可重复用于其他输入字段。 And it no longer matters when your code is loaded. 加载代码时不再重要。
function checkLength() { // Get current focused element const textBox = document.activeElement; if ( !textBox.value || textBox.value.length < 6 ) { textBox.style.backgroundColor = "red"; } else { textBox.style.backgroundColor = "#fff"; } }
<input type="text" name="random" onfocus="checkLength()" oninput="checkLength()">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.