form of validation js



  • There's a function to check the shape(s of the shape of the bere from the text_area fields) on the sabmit click. Stuck to the fact that when the fields are not filled, the form does not leave as it comes to being false. Show me how you can work it out.

    window.onload = function() {  
    
    d.getElementById('contact_sent').onclick = function(){
    var error_msg = "fill all fields to send the form";      
    var elements = document.getElementsByClassName('text_area');     
      for (var i = 0; i < elements.length; i++){
        if (elements[i].value == '' ){            
            elements[i].style.border = "2px solid orange";
            document.getElementById('error').innerHTML = error_msg;           
        }
      } 
    return false;   
    }       
    

    }



  • It's usually a flag in check, and then it's checked.

    d.getElementById('contact_sent').onclick = function(){
        var error_msg = "fill all fields to send the form";      
        var elements = document.getElementsByClassName('text_area');     
        var hasError = false;
        for (var i = 0; i < elements.length; i++){
            if (elements[i].value == '' ){     
                hasError = true;       
                elements[i].style.border = "2px solid orange";
                document.getElementById('error').innerHTML = error_msg;           
            }
        } 
        return !hasError;   
    }   
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2