admin管理员组文章数量:1310325
Here I have a problem which I can't get over with. This is the code and I want the script to change the form input's color when the entered text changes:
function checkName(name)
{
if (name.value == "value1" || name.value == "value2" || name.value == "value3" || name.value == "value4" || name.value == "value5" || name.value == "value6"){
document.forms['un'].elements['name'].style.color='#ffbb00'
}
else {
document.forms['un'].elements['name'].style.color='#000000'
}
}
As you can see when the entered text matches with one of the provided ones, it should turn orange, and when not, it shall be black. (I have an input toggling this function with an onChange thing) I just can't make it work so I can have multiple choices on when to change color, and also change back when the statement bees invalid. Any help?
Here I have a problem which I can't get over with. This is the code and I want the script to change the form input's color when the entered text changes:
function checkName(name)
{
if (name.value == "value1" || name.value == "value2" || name.value == "value3" || name.value == "value4" || name.value == "value5" || name.value == "value6"){
document.forms['un'].elements['name'].style.color='#ffbb00'
}
else {
document.forms['un'].elements['name'].style.color='#000000'
}
}
As you can see when the entered text matches with one of the provided ones, it should turn orange, and when not, it shall be black. (I have an input toggling this function with an onChange thing) I just can't make it work so I can have multiple choices on when to change color, and also change back when the statement bees invalid. Any help?
Share Improve this question edited Aug 29, 2013 at 10:30 Ozzy 8,3227 gold badges57 silver badges96 bronze badges asked Apr 20, 2012 at 21:15 SeinopSysSeinopSys 8,93710 gold badges65 silver badges114 bronze badges 2- Can you post your full code, please, perhaps in a jsFiddle? – SenorAmor Commented Apr 20, 2012 at 21:19
-
I haven't really read any source material to verify this, but shouldn't you be passing an integer to
document.forms[]
rather than a string'un'
? – David Thomas Commented Apr 20, 2012 at 21:19
4 Answers
Reset to default 2I have an input toggling this function with an onChange thing
so your Input
<input type=text size=20 name='name' onchange="checkName(this)">
and function could be
function checkName(el)
{
if (el.value == "value1" || el.value == "value2" || el.value == "value3" || el.value == "value4" || el.value == "value5" || el.value == "value6")
{
el.style.color='#ffbb00'
}
else
{
el.style.color='#000000'
}
}
An example.
You may also try onkeyup
like this example.
Try this instead:
function checkName(element) {
if (element.value == ...) {
element.className = "inputError";
} else {
element.className = "";
}
}
Then in your CSS file:
.inputError {
background-color: #FF0000;
background-image: url(exclamation_mark.png);
background-position: right 50%;
background-repeat: no-repeat;
background-size: contain;
-moz-background-size: contain;
-webkit-background-size: contain;
-o-background-size: contain;
}
Now that you have predefined CSS styles of the input boxes, everytime you check the input box you can set it to the appropriate CSS class name.
Now all you have to do is this:
<input id="myTextBox" type="text" onkeyup="javascript: checkName(this);" />
Hope this helps!
You assume that the value in the textbox is automatically passed in to the function checkName. I believe that this is your mistake.
You can refer to the value in the textbox within your function checkName
using event.srcElement.value
.
Here is one possible solution: http://jsfiddle/QJG5R/1/
Code dump:
var input = document.getElementById('blar');
var highlights = {
'v':'#888888',
'va':'#666699',
'val':'#4444aa',
'valu':'#2222bb',
'value':'#0000cc',
'value1':'#ff0000',
'value2':'#00ff00'
};
input.addEventListener('keyup', function(){
var value = input.value;
if (highlights[value]) {
input.style.color = highlights[value];
} else {
input.style.color = 'black';
}
});
Html contains a single element:
<input id='blar' type='text' />
本文标签: javascriptChecking form input for text and changing it39s colorStack Overflow
版权声明:本文标题:javascript - Checking form input for text and changing it's color - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741819431a2399274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论