All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Password Validation Using JavaScript Examples

Last Updated : Mar 11, 2024

Password Validation Using JavaScript Examples

In this tutorial we will show you the solution of password validation using JavaScript examples, it is very important to apply validations on passwords because a weak password is easy to crack and not a secured way to keep information private.

Hence, a password validation is necessary for strong password. Hence, in this tutorial we will understand about how to validation on password using JavaScript.

Step By Step Guide On Password Validation Using JavaScript Examples :-

Here, we are going to apply validation on password that it must contains following expression to make a strong password.

  • Must contain at least one Uppercase character
  • Must contain at least one Lowercase character
  • Must contain at least one digit
  • Must contain at least one special character
  • And it must be minimum of 8 characters.

Now, let us see the following example to see how to use above expression on password validation.

<!DOCTYPE html>
<html>
<head>
 <title>
  Password Validation
 </title>
</head>
<body>
 <p>
  <label>
   Fill your password here
  </label>
  <input type="text"
   placeholder="password"
   id="password" />
  <br/>
  <br/>
  <input type="button"
   value="Validate"
   onclick="test_password()" />
  <br/>
  <br/>
  <label>
   Confirmation message
  </label>
  <input type="text"
   id="status"
   readonly/>
 </p>
<script type="text/javascript">
  function test_ password () {
   var result;
   var pass =
    document.getElementById("password ").value;
   if (pass.match(/[a-z]/g) && pass.match(
     /[A-Z]/g) && pass.match(
     /[0-9]/g) && pass.match(
     /[^a-zA-Z\d]/g) && pass.length >= 8)
    result = "TRUE";
   else
    result = "FALSE";
   document.getElementById("status ").value = result;
  }
 </script>
</body>
</html>
  1. Here, above we show you an example in JavaScript.
  2. For this first of all we here create a basic structure of html. For this we use html, head, title and body with script tag. We hope you know about the basic structure of html and which html tag is used for which concept.
  3. Now, when we look inside body tag. Here, we see some headings tag and a script tag. Heading tag again is a concept of html, whereas script relates to JavaScript.
  4. JavaScript is written always inside script tag. Where script is a paired tag so it also has a closing tag. Now inside these script tag, we write our JavaScript code.
  5. Here, inside body as we see that we create two input fields. First one is to get the data from user and other is to display the output.
  6. Between both we create a button, on click on that button our function will be called.
  7. Now, inside JavaScript function. We get the value of user input and check that with help of if condition. If the data entered by user matches with our validation requirements then it will return a message with text True other it returns false. We hope you understand how to apply validation on password and these steps may help you for better understanding of codes.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to apply validation on password in JavaScript.

I hope this tutorial on password validation using JavaScript examples helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪