React Js Generate OTP
React Js Generate OTP | 6 Digit OTP Example:In ReactJS, you can create a 6-digit OTP (One-Time Password) by defining a function to generate a random number between 100000 and 999999. Utilize the JavaScript method Math.random()
to produce a random decimal value, then multiply and round it to get the desired 6-digit number. Finally, call this function to obtain the OTP for further use in your application.




Thanks for your feedback!
Your contributions will help us to improve service.
How can I generate a 6-digit OTP (One-Time Password) using Reactjs?
This React.js code generates a 6-digit OTP (One-Time Password) using the 'useState' hook. When the "Generate OTP" button is clicked, it triggers the 'generateOTP' function, which creates a random 6-digit OTP using the characters "0123456789". The generated OTP is displayed on the page below the button.
React Js Generate OTP | 6 Digit OTP Example
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
function App() {
const [otp, setOTP] = useState("");
const generateOTP = () => {
const digits = "0123456789";
let OTP = "";
for (let i = 0; i < 6; i++) {
OTP += digits[Math.floor(Math.random() * 10)];
}
setOTP(OTP);
};
return (
<div className="container">
<h3>React Js Generate OTP | 6 digit Otp</h3>
<button onClick={generateOTP}>Generate OTP</button>
<p>Generated OTP: {otp}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>