Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to validate a form and check for empty field and field length in JavaScript

1 Answer

0 votes
<!DOCTYPE html> 
<html> 
<script>
function validate() {  
    var name = document.loginform.name.value;  
    var password = document.loginform.password.value;  

    if (name === null || name === "") {   
      alert("Name is empty");  
      return false;  
    } else if (password.length < 8) {  
                alert("Password is less than 8 characters");  
                return false;  
           }  
}  
</script>  
<body>
<form name="loginform" method="post" action="login.php" onsubmit="return validate()" >  
    Name: <input type="text" name="name"><br/>  
    Password: <input type="password" name="password"><br/>  
<input type="submit" value="register">  
</form>
</body> 
</html>

 



answered Oct 10, 2019 by avibootz
...