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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if(textBox.value=='' || textLength<3 || textLength>6) { alert('Please enter correct password'); } else { alert('thank you for nice password'); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<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> |
Topic Covered
Setting the password length in JavaScript – Source code example with form validation. List of All Programs of Javascript : Click Here .