Password Validation Form Using JavaScript Last Updated : 18 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The password Validation form is used to check the password requirements such as the password must have at least one Uppercase, or lowercase, number, and the length of the password. We can also check this after submitting the form but it's not recommended. We can easily check before submitting the form this can save us time.In this article, we are going to make a Password validation form using JavaScript. Before going to start making the feature first we will talk about what functionalities we want in this project.Functionalities we want:While entering the password a message will be displayed.The message will alert the user to enter the password requirements.If the user doesn't enter the password according to the requirements it will show the message to enter the desired requirements.Here is the preview of the feature that we are going to make:Password Validation FormProject Structure:- index.html- style.css- script.jsExample: In this example code, we have implemented the above-discussed functionality.HTML Code: This file contains the structure of the featureCSS Code: It defines the styles for a form container and its elements, including input fields and a submit button. Additionally, it includes styles for a password message box and error messages.JavaScript Code: It handles password validation in a web form and performs the checks on the password input field and updates the corresponding password message items based on certain criteria. HTML <!-- index.html file --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Password Validation Form using javaScript</title> </head> <body> <div class="container"> <h3>Password Validation Form using JavaScript</h3> <form action="#"> <label for="username">Username</label> <input type="text" id="username" name="username" required> <label for="password">Password</label> <input type="password" id="password" name="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number, one uppercase and lowercase letter, and at least 8 characters" required> <div id="password-message"> <h3>Password must contain:</h3> <p class="password-message-item invalid">At least <b>one lowercase letter</b> </p> <p class="password-message-item invalid">At least <b>one uppercase letter</b> </p> <p class="password-message-item invalid">At least <b>one number</b> </p> <p class="password-message-item invalid">Minimum <b>8 characters</b> </p> </div> <input type="submit" value="Submit"> </form> </div> <script src="script.js"></script> </body> </html> CSS /* Style.css File */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Styling the container */ .container { margin: 0 auto; padding: 20px; max-width: 400px; background-color: #ffffff; } /* Style all input fields */ input { width: 100%; padding: 12px; margin: 6px 0 16px 0; border: 1px solid #ccc; border-radius: 4px; } /* Style the submit button */ input[type=submit] { background-color: #0f0fe9; color: white; cursor: pointer; } input[type=submit]:hover { background-color: #4141fc; } /* The message box */ #password-message { display: none; background: #ffffff; color: #000; position: relative; padding: 20px; margin-top: 10px; text-align: left; } #password-message h3 { font-size: 15px; margin-top: 0; text-transform: uppercase; } #password-message p { margin: 8px 0; } .valid { color: green; } .valid:before { position: relative; left: -35px; content: "✔"; } .invalid { color: red; } .invalid:before { position: relative; left: -35px; content: "✖"; } /* Error message style */ .error-msg { color: red; font-size: 14px; margin-top: 4px; } JavaScript // script.js File var passwordInput = document.getElementById("password"); var passwordMessageItems = document.getElementsByClassName("password-message-item"); var passwordMessage = document.getElementById("password-message"); passwordInput.onfocus = function () { passwordMessage.style.display = "block"; } // After clicking outside of password input hide the message passwordInput.onblur = function () { passwordMessage.style.display = "none"; } passwordInput.onkeyup = function () { // checking uppercase letters let uppercaseRegex = /[A-Z]/g; if (passwordInput.value.match(uppercaseRegex)) { passwordMessageItems[1].classList.remove("invalid"); passwordMessageItems[1].classList.add("valid"); } else { passwordMessageItems[1].classList.remove("valid"); passwordMessageItems[1].classList.add("invalid"); } // checking lowercase letters let lowercaseRegex = /[a-z]/g; if (passwordInput.value.match(lowercaseRegex)) { passwordMessageItems[0].classList.remove("invalid"); passwordMessageItems[0].classList.add("valid"); } else { passwordMessageItems[0].classList.remove("valid"); passwordMessageItems[0].classList.add("invalid"); } // checking the number let numbersRegex = /[0-9]/g; if (passwordInput.value.match(numbersRegex)) { passwordMessageItems[2].classList.remove("invalid"); passwordMessageItems[2].classList.add("valid"); } else { passwordMessageItems[2].classList.remove("valid"); passwordMessageItems[2].classList.add("invalid"); } // Checking length of the password if (passwordInput.value.length >= 8) { passwordMessageItems[3].classList.remove("invalid"); passwordMessageItems[3].classList.add("valid"); } else { passwordMessageItems[3].classList.remove("valid"); passwordMessageItems[3].classList.add("invalid"); } } Output: Comment More infoAdvertise with us Next Article Password Validation Form Using JavaScript R rahulkwh Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to validate confirm password using JavaScript ? In almost every modern website we have seen a login and signup feature, which is surely an important service for both the client and the service provider. When a user signs up for a certain website they have to put in a username a password, to enter the password websites ask them to enter a password 3 min read Validate a password using HTML and JavaScript Validating a password using HTML and JavaScript involves ensuring that user-entered passwords meet certain criteria, such as length, complexity, or character types (e.g., uppercase, lowercase, numbers, and symbols). This process enhances security by enforcing strong password requirements before form 2 min read Create a Password Validator using Bootstrap & JavaScript A password validator assesses password strength by verifying if it meets predetermined criteria like length, inclusion of uppercase and lowercase letters, digits, and symbols. It ensures robust security measures for user authentication and data protection. PrerequisitesHTMLBootstrapJavaScriptApproac 3 min read JavaScript Form Validation JavaScript form validation checks user input before submitting the form to ensure it's correct. It helps catch errors and improves the user experience.What we are going to CreateIn this article, we will guide you through creating a form validation application. This application will validate essentia 10 min read Form Validation using jQuery Form validation is a process of confirming the relevant information entered by the user in the input field. In this article, we will be validating a simple form that consists of a username, password, and a confirmed password using jQuery.Below are the approaches for validation of form using jQuery:T 5 min read Form validation using the jbvalidator Plugin jbvalidator is a jQuery and Bootstrap based plugin which supports both client and server form validations. The provided HTML data attributes are easy to use and understand. The plugin gives multi-language facilities along with custom validation rules and messages.The plugin can be used by downloadin 4 min read Password Matching using JavaScript In an online application form or social media signup page, it's common to have two input fields: one for the password and another for confirming the password. The goal is to check whether the entered passwords match.To achieve this, the first password is stored in a variable password1, and the confi 2 min read How to Toggle Password Visibility using HTML and JavaScript ? In this article, we will learn about how to toggle password visibility using HTML and JavaScript. Passwords are those input types that take hidden inputs. It can be shown and hidden from the user by toggling its type between text and password. Approach The following approach will be implemented to t 2 min read Show And Hide Password Using JavaScript When creating login or registration forms, a common feature is the ability to show and hide passwords. This improves user experience by allowing users to verify their input before submitting the form.What We're Going to CreateWe will create a "Password Hide and Show" feature that will allow users to 4 min read How to Toggle Password Visibility in JavaScript ? In this article we will discuss how to toggle password visibility in JavaScript we will also use HTML and CSS in order to create a password visibility toggler for our webpage. Approach: The basic approach towards making password visibility toggler will be quite simple, we will use a button and write 4 min read Like