Setting the password length in JavaScript – Source code example with form validation 

Setting the password length in JavaScript – Source code example with form validation 

In this tutorial, we will try to learn that how to set the password length in JavaScript and how to validate the form.

The logic for Setting the password length in JavaScript

First of all, we will find the length of the password and then validate the length by using the following logic;

if(textBox.value=='' || textLength<3 || textLength>6)

{

alert('Please enter correct password');

}

else

{

alert('thank you for nice password');

}

Program for Setting the password length in JavaScript

<html>
<head>
 <script type="text/javascript">
        function passwordLength()
                    //passwordLength is user define function define by us.
                    //function is keyword  
{
     var textBox = document.getElementById("password");
                    //var is keyword representing the variable.
                    //textbox is name of a variable
                  
       var textLength = textBox.value.length;
                    //getting the length of textbox and store the length in variable textLength 

         if(textBox.value=='' || textLength<3 || textLength>6)
                   //password length is valid only if password length is greater than 2 and less than 7.
         {
          alert('Please enter correct password');
         }
		 else
		 {
		 alert('thank you for nice password');
		 }
}
    </script>
	</head>
<body>
 Enter password:
<input type="password" id="password" />
<input type="button" id="btnSubmit" value="Submit the password" onclick="return passwordLength()" />
          
</body>
</html>

There are many other methods to validate the length of the password. You can also validate the length using regular expression and JQuery.

Hopefully, now, you are able to write a program for validating the password length in javascript.

Topic Covered

Setting the password length in JavaScript – Source code example with form validation. 
 
List of All Programs of Javascript : Click Here 
.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.