A simple utility for generating random One-Time Passwords (OTPs) in JavaScript. Generates a random OTP of the specified length. It is designed to be simple and efficient, making it easy to integrate into your applications.
- Generates Numeric OTPs: The generated OTPs consist of numeric digits only.
-
Customizable Length: You can specify the length of the OTP, with a default value of
6
. - Simple and Easy to Use: The function is straightforward to integrate into your applications.
- Handles Invalid Inputs Gracefully: The function throws an error for invalid lengths, ensuring robust error handling.
To use this utility, you can simply copy the code into your project. If you are using Node.js, you can create a module as follows:
npm install otp-random-generator
Import the module in your Node.js project:
const { generateOTP } = require('otp-random-generator');
const otp6 = generateOTP();
console.log('6-digit OTP:', otp6);
const otp8 = generateOTP(8);
console.log('8-digit OTP:', otp8);
const otp4 = generateOTP(4);
console.log('4-digit OTP:', otp4);
6-digit OTP: 394857
8-digit OTP: 48392057
4-digit OTP: 1204
The generateOTP
function generates a random OTP of the specified length. It is designed to be simple and efficient, making it easy to integrate into your applications.
-
length
(number): The length of the OTP to generate. The default value is6
. The length must be a positive integer.
- A string representing the generated OTP.
- An error if the
length
is less than or equal to0
.