I'm trying to build some JavaScript code that detects the users input in a textbox.
If the user enters, for example, the word 'test', I want the text/font colour to change to green.
Any other text that the user enters (such as 'tes', 'test1', 'testlm', 'biscuit', etc.) should change the text/font colour to red.
I've tried multiple ways of doing this with no avail. Please note that I am an absolute beginner with JavaScript and I am 'playing around' with code to help me learn it. Therefore, if the code is messy or completely wrong, I apologise for my lack of knowledge.
Here is the JavaScript code for the first test:
<script type="text/javascript">
function checkKey() {
var plaintext = document.getElementById("textbox");
var correct = input.style.color = '#66CD00';
var incorrect = input.style.color = '#FF0000';
if(plaintext =='test')
{
document.getElementById("textbox").innerHTML = correct;
}
else
{
document.getElementById("textbox").innerHTML = incorrect;
}
};
</script>
And the HTML code for the textbox:
<input type="text" id="textbox" name="plaintext" onKeyDown="checkKey()">
The second test is the same as the first, with the only difference in the JavaScript code being the 'var correct' and 'var incorrect' parts:
var correct = str.fontcolor("green");
var incorrect = str.fontcolor("red");
Both tests didn't work. I even took away the brackets in the HTML textbox code for the onKeyDown attribute for both tests:
<input type="text" id="textbox" name="plaintext" onKeyDown="checkKey">
Which again didn't work.
I was wondering if there's any way to achieve my aforementioned desired outcome? Have I done anything right in my experimental code?
Thanks in advance for your time (and sorry for the long question).