OTP Generator and Validator Card using JavaScript & Tailwind CSS Last Updated : 07 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report OTP (One-Time Password) generator and validator built using Tailwind CSS and JavaScript allows users to generate a random OTP and validate it against the entered OTP. The application has a user-friendly interface for generating and validating OTPs using JavaScript for interactivity and Tailwind CSS for styling. Approach to create OTP Generator and ValidatorIntegrate the Tailwind CSS via CDN Link in your HTML code. Use different HTML elements to structure the web page and style them using different Tailwind utilities.The JavaScript code at the bottom of the page adds functionality to the buttons. When the "Generate OTP" button is clicked, it generates a random OTP consisting of 6 characters (a mix of uppercase letters, lowercase letters, and numbers) and displays it in the OTP box.It also enables the "Validate OTP" button and clears any previous status message.When the "Validate OTP" button is clicked, it checks if the entered OTP matches the generated OTP. Depending on the result, it updates the status message accordingly and applies the appropriate text color (green for valid OTP, red for invalid OTP).Example: Implementation of Designing an OTP Generator and Validator in Tailwind CSS and JavaScript HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>OTP Generator and Validator</title> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet"> </head> <body class="bg-gray-100 flex justify-center items-center h-screen"> <div class="max-w-md mx-auto p-8 bg-white shadow-md rounded-lg border-2 border-green-400"> <h1 class="text-2xl font-semibold mb-5"> OTP Generator | Validator </h1> <button id="generate-otp" class="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 focus:outline-none"> Generate OTP </button> <div id="otp-box" class="hidden mt-4 bg-white border-2 border-gray-300 rounded-md p-4"> </div> <input id="user-input" type="text" class="mt-4 block w-full border-2 border-green-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500" placeholder="Enter OTP"> <button id="validate-otp" class="mt-4 px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 focus:outline-none" disabled> Validate OTP </button> <div id="otp-status" class="mt-4 text-sm text-gray-500"> </div> </div> <script> const generateOTPButton = document.getElementById('generate-otp'); const otpBox = document.getElementById('otp-box'); const userInput = document.getElementById('user-input'); const validateOTPButton = document.getElementById('validate-otp'); const otpStatus = document.getElementById('otp-status'); generateOTPButton.addEventListener('click', () => { let generatedOtp = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (let i = 0; i < 6; i++) { generatedOtp += characters.charAt(Math .floor(Math.random() * characters.length)); } otpBox.textContent = generatedOtp; otpBox.classList.remove('hidden'); validateOTPButton.disabled = false; otpStatus.textContent = ''; }); validateOTPButton.addEventListener('click', () => { const enteredOTP = userInput.value; const generatedOTP = otpBox.textContent; if (!enteredOTP) { otpStatus.textContent = 'Please enter OTP'; } else if (enteredOTP === generatedOTP) { otpStatus.textContent = 'Valid OTP'; otpStatus.classList.remove('text-red-500'); otpStatus.classList.add('text-green-500'); } else { otpStatus.textContent = 'Invalid OTP'; otpStatus.classList.remove('text-green-500'); otpStatus.classList.add('text-red-500'); } }); </script> </body> </html> Output: Output Comment More infoAdvertise with us S subramanyasmgm Follow Improve Article Tags : JavaScript Similar Reads 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 JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an 7 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav 15+ min read Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex 4 min read Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva 3 min read HTML DOM (Document Object Model) The HTML DOM (Document Object Model) is a programming interface that represents the structure of a web page in a way that programming languages like JavaScript can understand and manipulate. Think of it as a tree of objects where each part of your HTML document (elements, attributes, text) is repres 6 min read Like