Search code examples
javascripthtmlformsvalidationnextsibling

Why is the nextSibling method in javascript not working properly


I have this code that is setting innerHTML to the sibling element of input type [email] but not to the sibling element of input type [name].Here is the markup.

<div class="container">

<div>
  <input type="email" name="email" id="email" class="validate"/>
  <span class="email"></span>
</div>
<div>
  <input type="text" name="name" id="name" class="validate"/>
  <span class="name"></span>
</div>
<input type="submit" value="Download" id="install"/>  

</div>

This is the markup part.You see there is input with sibling element span.

Following is the javasript code that validates email & name and outputs errorText if any to the respective sibling element.

window.addEventListener("load",ready);

function _x(elem){
  return document.getElementById(elem) || document.getElementsByClassName(elem);
}

function ready(){

    function Start(){
        var call      = this;
        this.expEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;  
        this.button   = _x("install");
        this.field    = _x("validate");

        this.clicks    = function(){
            for(var i=0;i<this.field.length;i++){
            this.field[i].addEventListener("keyup",this.validate);
        }
    };

    this.validate = function(){
        call.check(this);
    }

    this.check    = function(field){
        var value = field.value,
            error = "",
            sibli = field.nextSibling; //This is giving problem
        switch(field.name){
            case "email":
                error = (!this.minLength(value,12)) ? "Email must be greater then 3 characters":"";
                error = (!this.maxLength(value,24)) ? "Email cannot be more then 24 characters":"";
                error = (!this.valEmail(value,12)) ? "The email format is invalid":"";   
            break;
            case "name":
                error = (!this.minLength(value,3)) ? "Name must be greater then 3 characters":"";
                error = (!this.maxLength(value,24)) ? "Name cannot be more then 24 characters":""; 
            break; 
        }
        sibli.innerHTML = error; //outputs only for input type [email]
    };

    this.minLength = function(field,length){
        return (field.length>length);
    };

    this.maxLength = function(field,length){
        return (field.length<length);
    };

    this.valEmail  = function(field){
        return (this.expEmail.test(field));  
    };
}

var start = new Start();
    start.clicks();
}

Solution

  • Look at this part:

      error = (!this.minLength(value,3)) ? "Name must be greater then 3 characters":"";
      error = (!this.maxLength(value,24)) ? "Name cannot be more then 24 characters":""; 
    

    For the first line, this.minLength will return false since we're checking on each keyup, so you'd set error to "Name must be greater then 3 characters". Then in the second line, this.maxLength will return true which then with the "!" operator results in false and your error gets set to "" making sibli.innerHTML = "";

    So basically you want to do:

        case "name":
    
            if (!this.minLength(value,3)) {
                error = "Name must be greater then 3 characters";
            } else if(!this.maxLength(value,24)) {
                error = "Name cannot be more then 24 characters";
            }
    
        break;