Open In App

Build a PIN Pad using HTML CSS & JavaScript

In this article, we will see how can we implement a PIN pad for entering and checking whether the PIN is valid with the help of HTML, CSS, and JavaScript.

Here, we have provided a numeric keypad/PIN pad through which the user can input the PIN. Once the user enters a PIN and hits “OK”, the code validates the PIN and provides an alert on whether the PIN is correct or not.



Preview of final output: Let us have a look at how the final output will look like.



Prerequisites

Approach & Handled Use Cases

Project Structure

Project Structure

Example:The below example code implements a basic PIN pad application using just HTML, CSS and JavaScript.




// Script.js
// Correct Pin Value
let correctPin = "1234";
 
let btns =
    document.getElementsByClassName(
        "pinpad-btn"
    );
let pinInput = document.getElementById(
    "pinpad-input"
);
 
for (let i = 0; i < btns.length; i++) {
    let btn = btns.item(i);
    if (
        btn.id &&
        (btn.id === "submit-btn" ||
            btn.id === "delete-btn")
    )
        continue;
 
    // Add onclick event listener to
    // Every button from 0 - 9
    btn.addEventListener(
        "click",
        (e) => {
            pinInput.value +=
                e.target.value;
        }
    );
}
 
let submitBtn = document.getElementById(
    "submit-btn"
);
let delBtn = document.getElementById(
    "delete-btn"
);
let modal =
    document.getElementById("modal");
let result =
    document.getElementById("result");
let closeBtn =
    document.getElementById("close");
 
submitBtn.addEventListener(
    "click",
    () => {
        if (
            !pinInput ||
            !pinInput.value ||
            pinInput.value === ""
        ) {
            alert(
                "Please enter a pin first"
            );
        } else if (
            pinInput.value ===
            correctPin
        ) {
            alert("Correct PIN");
        } else {
            alert("Incorrect PIN");
        }
        // Reset the input
        pinInput.value = "";
    }
);
 
delBtn.addEventListener("click", () => {
    if (pinInput.value)
        pinInput.value =
            pinInput.value.substr(
                0,
                pinInput.value.length -
                    1
            );
});
 
closeBtn.addEventListener(
    "click",
    () => {
        modal.style.display = "none";
    }
);




<!-- Index.html -->
<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>Pin Pad</title>
    <link rel="stylesheet"
          href="./style.css"/>
</head>
 
<body>
    <h2 class="text-center">Pin Pad</h2>
    <div class="container">
        <div class="card">
            <div class="card-header">
                <input type="password"
                       id="pinpad-input"
                       readonly />
            </div>
            <div>
                <div class="row">
                    <button type="button"
                            class="pinpad-btn"
                            value="1">
                            1
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="2">
                            2
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="3">
                            3
                    </button>
                </div>
                <div class="row">
                    <button type="button"
                            class="pinpad-btn"
                            value="4">
                            4
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="5">
                            5
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="6">
                            6
                    </button>
                </div>
                <div class="row">
                    <button type="button"
                            class="pinpad-btn"
                            value="7">
                            7
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="8">
                            8
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="9">
                            9
                    </button>
                     
                </div>
                <div class="row">
                    <button type="button"
                            class="pinpad-btn"
                            value="del
                            "
                         id="delete-btn">
                        Del
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="0">
                            0
                    </button>
                    <button type="button"
                            class="pinpad-btn"
                            value="ok"
                            id="submit-btn">
                        Ok
                    </button>
                </div>
            </div>
        </div>
 
        <div id="modal" class="modal">
            <div class="modal-body">
                <span id="close">×</span>
                <p id="result"></p>
            </div>
        </div>
    </div>
        <script src="./script.js"></script>
</body>
 
</html>




/* Style.css */
.text-center {
    text-align: center;
}
 
.container {
    width: 100%;
}
 
.card {
    border: 1px solid black;
    border-radius: 5px;
    width: 30%;
    margin-right: auto;
    margin-left: auto;
    display: flex;
    flex-direction: column;
}
 
.card-header {
    display: flex;
    justify-content: center;
    margin: 10px;
    text-align: center;
}
 
.row {
    display: flex;
    flex-direction: row;
    justify-content: center;
}
 
.pinpad-btn {
    width: 25%;
    height: 75px;
    margin: 5px;
    padding: 5px;
    border: 1px solid black;
    border-radius: 20%;
    font-size: 2em;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    background-color: white;
}
 
.pinpad-btn:hover {
    background-color: lightgray;
}
 
#pinpad-input {
    border-radius: 10px;
    height: 1.5em;
    font-size: 1.5em;
    text-align: center;
    width: 80%;
}

Steps to Run application: Open the Live Server and then Please enter following URL in the link

http://localhost:5500/

Output:

Output GIF


Article Tags :