1

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>
cweiske
  • 30,033
  • 14
  • 133
  • 194
Nexz
  • 93
  • 1
  • 10
  • 1
    Typo: `function matchWord` (camelCase) vs `= matchword` (lowercase). – Jonathan Lonowski Mar 08 '17 at 07:08
  • just fix it but still won't work. – Nexz Mar 08 '17 at 07:12
  • 1
    Have you checked your browser's devtools for errors? One that's possible is explained by "[Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element)" – Jonathan Lonowski Mar 08 '17 at 07:13
  • Try putting the Script in the body. Or at least in the tags! Edit: and give it a tag to be safe. – Adam Patterson Mar 08 '17 at 07:21

6 Answers6

2

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>
Saurabh Sharma
  • 2,422
  • 4
  • 20
  • 41
2

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>
m87
  • 4,445
  • 3
  • 16
  • 31
1

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()">
Juan
  • 3,675
  • 20
  • 34
1

There are a few errors in the sample you provided.

  • First off, names are case-sensitive, so matchWord is not the same as matchword. Be careful!
  • Secondly, you need to use document.getElementById('uname').onkeyup = matchWord; to assign the function itself to the event, not the result that it returns.
  • Third and final point, the logic in your method is wrong, as you use 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>
Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
0
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>
Viji K
  • 24
  • 3
  • 3
    Tip: Try to include at least a brief explanation along with a snippet, detailing what's different from the snippet(s) in the question and why you think the revisions are necessary. – Jonathan Lonowski Mar 08 '17 at 07:32
0

Put:

<script> // stuff </script>

in:

<html> // here </script>
Adam Patterson
  • 958
  • 7
  • 13