Create an OTP Verification Form in HTML CSS & JavaScript Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report When the application is loaded, there is a button through which the user can generate the OTP. Once the user generates the OTP, the form for verification becomes visible, the user needs to enter the correct OTP that is generated. When the user enters the OTP in the input field, and if the OTP matches then the OTP gets verified and the user can generate more OTP. If the OTP is not matched, then the Generate OTP button remains disabled til the OTP is been verified. Preview Image:Approach:Firstly, create the overall layout or the structure of the application using HTML elements like <div>, <h1>, <button>, <input> etc.When the structure becomes ready, then the next task is to style the application with attractive styling properties of CSS. Some of the styling properties that we have used to style the application are background-color, margin, text-align, font-size etc. Once the styling of the application is done, then we can write the code for the functionality of verification in the JavaScript file. Using the Math.floor() and Math.random() functions in JavaScript, we generate the 4-digit numerical OTP when the Generate OTP button is clicked by the user. Using the if-else conditions, we are verifying the OTP by checking the Generated OTP and Inputted OTP matching. If this matches then the OTP gets verified and the Generate OTP button gets enabled for the user to generate more OTP. Example: This example describes the basic implementation for an OTP Verification Form in HTML, CSS & JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>GFG OTP Verification</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap"> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <div class="card"> <div class="header"> <h1 style="color:green"> GeeksforGeeks </h1> <h2 style="color:black"> OTP Verification </h2> </div> <div class="content" id="content"> <button id="generateBtn" onclick="OTPFn()"> Generate OTP </button> <div id="otpForm" class="otp-form"> <input type="text" id="userOTP" placeholder="Enter OTP"> <button onclick="OTPVerifyFn()"> Verify </button> </div> <div id="successMessage" class="success-message"> <i class="fas fa-check"></i> <p>Congratulations Geek! OTP is Verified!</p> </div> <div id="errorMessage" class="error-message"></div> <div id="timer" class="timer"></div> </div> </div> </div> <script src="script.js"></script> </body> </html> CSS body { font-family: 'Montserrat', sans-serif; margin: 0; display: flex; align-items: center; justify-content: center; height: 100vh; background: linear-gradient(to right, #667eea, #764ba2); } .container { width: 100%; max-width: 400px; } .card { background-color: #fff; border-radius: 10px; overflow: hidden; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); } .header { background-color: #fbf96f; color: #fff; padding: 20px; text-align: center; } .content { padding: 20px; } button { background-color: #4CAF50; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #45a049; } .otp-form { display: none; flex-direction: column; align-items: center; margin-top: 20px; } input { width: calc(100% - 30px); padding: 10px; margin-bottom: 10px; border: 1px solid #ddd; border-radius: 5px; outline: none; } .success-message { display: none; flex-direction: column; align-items: center; margin-top: 20px; color: #4CAF50; } .error-message { color: #e74c3c; margin-top: 10px; } button:disabled { background-color: #ccc; cursor: not-allowed; } .success-message p { margin: 10px 0; } .otp-display { margin-top: 20px; text-align: center; } .otp-text { font-size: 18px; margin: 0; } input:disabled { background-color: #f2f2f2; color: #a0a0a0; cursor: not-allowed; } .timer { margin-top: 10px; font-size: 14px; } JavaScript let otpGen; let timer; let secondsRemaining = 10; function OTPFn() { const btn = document.getElementById('generateBtn'); btn.disabled = true; clearFn(); otpGen = Math.floor(1000 + Math.random() * 9000); const temp = document.getElementById('content'); const showOtp = document.createElement('div'); showOtp.classList.add('otp-display'); showOtp.innerHTML = ` <p class="otp-text">Generated OTP: <span>${otpGen}</span> </p>`; temp.appendChild(showOtp); document.getElementById('otpForm').style.display = 'flex'; startTimer(); } function clearFn() { const prevOtp = document.querySelector('.otp-display'); if (prevOtp) { prevOtp.remove(); } resetTimer(); document.getElementById('errorMessage').innerText = ''; enableInputField(); } function OTPVerifyFn() { const userOtp = document.getElementById('userOTP').value; if (userOtp === "") { alert("Please enter OTP."); return; } const enterOtp = parseInt(userOtp); if (!isNaN(enterOtp)) { if (secondsRemaining > 0) { if (enterOtp === otpGen) { showMsgFn(); document.getElementById('generateBtn').disabled = false; resetTimer(); enableInputField(); } else { document.getElementById('errorMessage').innerText = 'Invalid OTP. Please try again.'; } } else { document.getElementById('errorMessage').innerText = 'OTP Expired. Please generate a new OTP.'; resetTimer(); } } else { alert("Invalid OTP. Please try again."); } } function showMsgFn() { const successMessage = document.getElementById('successMessage'); successMessage.style.animation = 'fadeIn 1s forwards'; successMessage.style.display = 'flex'; setTimeout(() => { successMessage.style.display = 'none'; }, 3000); } function startTimer() { timer = setInterval(function () { if (secondsRemaining <= 0) { clearInterval(timer); document.getElementById('generateBtn').disabled = false; document.getElementById('errorMessage').innerText = 'OTP Expired. Please generate a new OTP.'; resetTimer(); disableInputField(); } else { document.getElementById('timer').innerText = `Time Remaining: ${secondsRemaining} seconds`; secondsRemaining--; } }, 1000); } function resetTimer() { clearInterval(timer); document.getElementById('timer').innerText = ''; secondsRemaining = 10; } function disableInputField() { document.getElementById('userOTP').disabled = true; } function enableInputField() { document.getElementById('userOTP').disabled = false; } function clearFields() { document.getElementById('userOTP').value = ''; clearFn(); } Output: Comment More infoAdvertise with us Next Article Create an OTP Verification Form in HTML CSS & JavaScript G gpancomputer Follow Improve Article Tags : Project JavaScript Web Technologies Dev Scripter JavaScript-Projects Dev Scripter 2024 +2 More Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Like