Reputation: 93
I have tried coding some javascript for form that, when user typing certain word then the textfield below will automatically disable.The problem is, when i the tried the code in jsfiddle the code will run smoothly but when i use it in my project, it don't work. Can anybody explain what wrong with my code.
<script>
function matchWord(e){
if(document.getElementById('uname').value.trim === 'Unknown' || document.getElementById('uname').value.trim === 'unknown'){
document.getElementById('pword').disabled = true;
document.getElementById('response').disabled = true;
alert('you enter bannned name in the field');
}
else{
}
}
document.getElementById('uname').onkeyup = matchWord();
</script>
<html>
<body>
Name:<input type="text" name="uname" id="uname" placeholder="Username"><br>
password:<input type="password" name='pword' id='pword' placeholder="password"><br>
Responsibility:<select id = "response">
<option>---Responsibility---</option>
</select>
</body>
</html>
Upvotes: 1
Views: 94
Reputation: 6707
There are a few errors in the sample you provided.
matchWord
is not the same as matchword
. Be careful!document.getElementById('uname').onkeyup = matchWord;
to assign the function itself to the event, not the result that it returns. trim
instead of trim()
, which is the exact opposite mistake of the one mentioned above and this returns the function instead of the value. The following snippet works:function matchWord(e) {
if (document.getElementById('uname').value.trim() === 'Unknown' ||
document.getElementById('uname').value.trim() === 'unknown') {
document.getElementById('pword').disabled = true;
document.getElementById('response').disabled = true;
alert('you enter bannned name in the field');
} else {}
}
document.getElementById('uname').onkeyup = matchWord;
Name:<input type="text" name="uname" id="uname" placeholder="Username"><br> password:
<input type="password" name='pword' id='pword' placeholder="password"><br> Responsibility:
<select id="response">
<option>---Responsibility---</option>
</select>
Upvotes: 1
Reputation: 958
Put:
<script> // stuff </script>
in:
<html> // here </script>
Upvotes: 0
Reputation: 4511
First trim() is not a property, its a method. So, you need to change it from trim
to trim()
.
Second you need to assign a function to onkeyup
, not call a function.
function matchWord(e) {
if (document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim === 'unknown') {
document.getElementById('pword').disabled = true;
document.getElementById('response').disabled = true;
alert('you enter bannned name in the field');
} else {}
}
document.getElementById('uname').onkeyup = matchWord;
Name:<input type="text" name="uname" id="uname" placeholder="Username"><br>
password:<input type="password" name='pword' id='pword' placeholder="password"><br>
Responsibility:
<select id = "response">
<option>---Responsibility---</option>
</select>
Upvotes: 2
Reputation: 2472
Problem 1: Typo matchword()
, replace it with matchWord();
Problem 2: trim is a method! use document.getElementById('uname').value.trim()
instead.
function matchWord(e) {
if (document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim() === 'unknown') {
document.getElementById('pword').disabled = true;
document.getElementById('response').disabled = true;
alert('you enter bannned name in the field');
}
}
Name:
<input type="text" name="uname" id="uname" placeholder="Username" onkeyup="matchWord()">
<br> password:
<input type="password" name='pword' id='pword' placeholder="password">
<br> Responsibility:
<select id="response">
<option>---Responsibility---</option>
</select>
Upvotes: 2
Reputation: 24
Below code works fine for me in all browsers
<html>
<body>
Name:<input type="text" name="uname" id="uname" placeholder="Username"><br>
password:<input type="password" name='pword' id='pword' placeholder="password"><br>
Responsibility:<select id = "response">
<option>---Responsibility---</option>
</select>
</body>
<script>
function matchWord(e){
if(document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim() === 'unknown'){
document.getElementById('pword').disabled = true;
document.getElementById('response').disabled = true;
alert('you enter bannned name in the field');
}
else{
}
}
document.getElementById('uname').addEventListener("keyup", function(evt) {
matchWord(evt);
}, false);
</script>
</html>
Upvotes: 0
Reputation: 3705
You are setting as eventhanlder of the KeyUp event the results of your function, and you want to set the function itself.
Try:
document.getElementById('uname').onkeyup = matchWord;
Instead, or add the event handler in the element itself, removing previous line and adding:
<input type="text" name="uname" id="uname" placeholder="Username" onkeyup="matchWord()">
Upvotes: 1