Open In App

Validate a password using HTML and JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

The task is to validate the password using HTML and JavaScript.

A password is correct if it contains:

  1. At least 1 uppercase character.
  2. At least 1 lowercase character.
  3. At least 1 digit.
  4. At least 1 special character.
  5. Minimum 8 characters.

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>validate password</title>
    <script type="text/javascript">
        function test_str() {
            var res;
            var str =
                document.getElementById("t1").value;
            if (str.match(/[a-z]/g) && str.match(
                    /[A-Z]/g) && str.match(
                    /[0-9]/g) && str.match(
                    /[^a-zA-Z\d]/g) && str.length >= 8)
                res = "TRUE";
            else
                res = "FALSE";
            document.getElementById("t2").value = res;
  
        }
    </script>
</head>
  
<body>
    <p>
        String:
        <input type="text" 
               placeholder="abc"
               id="t1" />
        <br/>
        <br/>
        <input type="button"
               value="Check" 
               onclick="test_str()" />
        <br/>
        <br/> Output:
        <input type="text" 
               id="t2"
               readonly/>
    </p>
</body>
  
</html>


Output:




Last Updated : 16 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads