-1

Is it something wrong with this script?

function validation() {
  var message, x;
  message = document.getElementsByClassName("message");
  message.innerHTML = "";
  x = document.getElementsByClassName("inputNumber").value;

  try { 

      if(isNaN(x)) throw "Input is not a number";

  }
  catch(err) {
      message.innerHTML = err;
  }

}

The HTML is this:

    <input type="number" class="form-control inputNumber" onkeyup="validation()" >
    <div class="help-block with-errors"></div>
    <p class="message"></p>

The console doesn't show any errors but the validation doesn't happen.

4
  • 4
    The .getElementsByClassName() function returns a list of elements. You can't treat the return value as if it were a single element. Commented Sep 8, 2015 at 13:08
  • Here are some docs on NodeList and HTMLCollection Commented Sep 8, 2015 at 13:09
  • so should I use ".getElementById()" ? What if I have more inputs that requests numbers? I can't give the same ID. Sorry, I am a beginner. Commented Sep 8, 2015 at 13:12
  • Forms have an elements collection that is all the controls in the form. You can iterate over that for validation. Commented Sep 9, 2015 at 10:15

2 Answers 2

1

Try this:

Just for testing purpose I have set type='text'. I have passed the current context of the input field as an argument so that I can relate other closest elements using that context.

function validation(elem) {
  var message, x;
  message = elem.nextElementSibling.nextElementSibling;
  message.innerHTML = "";
  x = elem.value;
  if (isNaN(x)) {
    message.innerHTML = "Input is not a number";
  }
}
<input type="text" class="form-control inputNumber" onkeyup="validation(this)">

<div class="help-block with-errors"></div>
<p class="message"></p>

Sign up to request clarification or add additional context in comments.

5 Comments

thank you, it works. Can you explain why you used 'onkeyup="validation(this)" ' ? why "(this)" ?
@ValeriaMelinte, If you have only one text field then you can have id to that element and can manipulate your DOM using document.getElementById(yourId)
I have more, that's why I'm using classes
If you have multiple elements then you must have some identifier for your element. And this is the reason I am passing this as an argument to identify that element.
0

The .getElementsByClassName() function returns a list of elements. So you have to treat them as array Change your code with this

message = document.getElementsByClassName("message");
message[0].innerHTML = "";
 x = document.getElementsByClassName("inputNumber")[0].value;

3 Comments

This could still be error prone if he has other elements that share that class name.
What if there are multiple input boxes with multiple error message containers ?
yes but by given code we can solve this by using it .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.