Introduction
While developing a website with a login page, you also need to make a signup page. During the signup process, they need to enter a new password which should contain some type of rules. When creating a password like that, you need to validate the password in the sense that it satisfies the rules for creating a password. Let us discuss the validation of those password processes in this article.
IDE Used
For this, I am using Visual Studio Code for developing this webpage. It is a tool that helps you to easily edit code. You can use this tool for running programs with different languages. It helps you with coding by providing some autosuggestions based on your coding language.
Steps To Create a Validation Page
Step 1
Open a new file in VS code by File - New File, or by clicking ctrl+N.
Step 2
The file appears without any extension, so it will be considered plain text.
Rename the file and store it under a .html extension by using the save as option.
Step 3
You can type code under that .html file.
Code for the page is:
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content"width=device-width, initial-scale=1">
- <style>
-
- input {
- width: 100%;
- padding: 15px;
- border: 1px solid rgb(69, 113, 194);
- border-radius: 4px;
- box-sizing: border-box;
- margin-top: 6px;
- margin-bottom: 16px;
- }
-
-
- input[type=submit] {
- background-color: rgba(51, 27, 160, 0.925);
- color: white;
- }
-
-
- .container {
- background-color: rgba(34, 111, 226, 0.356);
- padding: 20px;
- }
-
-
- #message {
- display:none;
- background: rgba(34, 111, 226, 0.356);
- color: rgb(0, 0, 0);
- position: relative;
- padding: 20px;
- margin-top: 10px;
- }
-
- #message p {
- padding: 10px 35px;
- font-size: 18px;
- }
-
-
- .valid {
- color: rgb(34, 111, 146);
- }
-
- .valid:before {
- position: relative;
- left: -35px;
- content: "โ";
- }
-
-
- .invalid {
- color: red;
- }
-
- .invalid:before {
- position: relative;
- left: -35px;
- content: "โ";
- }
- </style>
- </head>
- <body>
-
- <h3>Password Validation with certain condition</h3>
- <p>Try to submit the form.</p>
-
- <div class="container">
- <form action="/action_page.php">
- <label for="usrname"><b>Username</b></label>
- <input type="text" id="usrname" name="usrname" required>
-
- <label for="psw"><b>New Password</b></label>
- <input type="password" id="psw" name="psw" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" required>
-
- <input type="submit" value="Submit">
- </form>
- </div>
-
- <div id="message">
- <h3>Password must contain the following:</h3>
- <p id="letter" class="invalid">A <b>lowercase</b> letter</p>
- <p id="capital" class="invalid">A <b>capital (uppercase)</b> letter</p>
- <p id="number" class="invalid">A <b>number</b></p>
- <p id="length" class="invalid">Minimum <b>8 characters</b></p>
- </div>
-
- <script>
- var myInput = document.getElementById("psw");
- var letter = document.getElementById("letter");
- var capital = document.getElementById("capital");
- var number = document.getElementById("number");
- var length = document.getElementById("length");
-
-
- myInput.onfocus = function() {
- document.getElementById("message").style.display = "block";
- }
-
-
- myInput.onblur = function() {
- document.getElementById("message").style.display = "none";
- }
-
-
- myInput.onkeyup = function() {
-
- var lowerCaseLetters = /[a-z]/g;
- if(myInput.value.match(lowerCaseLetters)) {
- letter.classList.remove("invalid");
- letter.classList.add("valid");
- } else {
- letter.classList.remove("valid");
- letter.classList.add("invalid");
- }
-
-
- var upperCaseLetters = /[A-Z]/g;
- if(myInput.value.match(upperCaseLetters)) {
- capital.classList.remove("invalid");
- capital.classList.add("valid");
- } else {
- capital.classList.remove("valid");
- capital.classList.add("invalid");
- }
-
-
- var numbers = /[0-9]/g;
- if(myInput.value.match(numbers)) {
- number.classList.remove("invalid");
- number.classList.add("valid");
- } else {
- number.classList.remove("valid");
- number.classList.add("invalid");
- }
-
-
- if(myInput.value.length >= 8) {
- length.classList.remove("invalid");
- length.classList.add("valid");
- } else {
- length.classList.remove("valid");
- length.classList.add("invalid");
- }
- }
- </script>
-
- </body>
- </html>
Step 4
After coding, you can save, then you can find it as an HTML page in the location where you stored it.
Step 5
You can open it in a browser.
After entering the password based on your rules, it will become green.
Conclusion
This gives you additional security for the users of your website by making them enter a strong password and secure their data. You can edit the rules based on your requirements.