How to match Two Password

Hello i am a beginner and started to learn Javascript…
i made a Userform with the help of Google but when i match ConfirmPassword to Password it does not match please show me where i am doing wrong in the below code:-

<!DOCTYPE html>
<html lang="en">

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href=""
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UserForm Validation with the help of Regular Expression</title>
    <script src=""
    <script src=""
    <script src=""

    <h1 class="text-center bg-dark text-white">UserForm Registration with the help of Regular Expression</h1>
    <div class="container">
        <form onsubmit="return validation()">

            <!-- Password -->
            <div class="form-group">
                <input type="text" name="" id="inputpassword" class="form-control" placeholder="Password">
                <span id="spanpassword" class="text-danger font-weight-bold"></span>

            <!-- ConfirmPassword -->
            <div class="form-group">
                <label>Confirm Password:</label>
                <input type="text" name="" id="inputconfirmpassword" class="form-control"
                    placeholder="Confirm Password">
                <span id="spanconfirmpassword" class="text-danger font-weight-bold"></span>

            <!-- EmailAddress -->
            <div class="form-group">
                <label>Email Address:</label>
                <input type="text" name="" id="inputemail" class="form-control" placeholder="Email">
                <span id="spanemail" class="text-danger font-weight-bold"></span>

            <!-- submit <button> -->
            <input type="submit" name="" class="btn btn-primary">

    <script type="text/JavaScript">
    function validation(){
            var password1 = document.getElementById("inputpassword").value;
            var password2 = document.getElementById("inputconfirmpassword").value;
            var email1 = document.getElementById("inputemail").value;

            // Validate through RegularExpression
            var passwordcheck1 =/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
            var emailcheck = /^[A-Za-z_]{3,}@[A-Za-z]{3,}[.]{1}[A-Za-z.]{2,6}$/;

if (passwordcheck1.test(password1)){
    document.getElementById("spanpassword").innerHTML="Invalid Password";
    return false;

    document.getElementById("spanconfirmpassword").innerHTML="No password matched";
    else {
    document.getElementById("spanconfirmpassword").innerHTML="Password matched";
    return false;

if (emailcheck.test(email1)){
    document.getElementById("spanemail").innerHTML="Enter valid email address";
    return false;




It works for me.

Sir when i enter password in Password Box:- 123@Jet1
then i enter Confirm Password :- 123@Jet (showing password matched, although both password are not same) …plese fix it

It works for me when I use a test password of Pa$$w0rd

There are sites that let you test regular expressions against test inputs.

Nothing seems wrong with how the regular expression tests against your password.

Taking a closer look at the code I see the problem:

  if (password1.match(password2)) {

password2 is not a regular expression, but the above code expects it to be one.
The match method is the wrong one to use there.

You should instead just do a comparison check of both passwords strings.

Thanks for reply… if
if (password1.match(password2)) {
is a wrong method then which method i will use (i am a begineer)…Sir can you modify my code if possible

Do you know how to use the === comparison?

Do you know how to use the === comparison?
yes i know but nothing work for me…i dont know why …
i already used below code one by one but i get result Zero
if (document.getElementById(“spanpassword”).value !==document.getElementById(“spanconfirmpassword”).value){

You already have both of the passwords available as the variables named password1 and password2.

You don’t need to use getElementById techniques at all to get this working.

Sir when i dont get desired output then i used different methods
problem is in Confirm Password…it giving me headache

i used
if (password1 !== password2){
document.getElementById(“spanconfirmpassword”).innerHTML=“Password not matched”;

The first body part of that if statement is clearing the message. That I expect is happening because the passwords match, but in the condition part you are checking that they don’t match.

Just use === in the condition to compare the passwords and that should start working.

Thankyou sir for give me right suggestion it is working “===”

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.