validis
Version:
Validation made simple! 🚀 This package gives you all the tools you need to quickly and effortlessly validate emails, phone numbers, passwords, character limits, and more. Whether you’re checking for a positive number, enforcing a specific range, or custo
21 lines (15 loc) • 825 B
JavaScript
/**
* Generates a random OTP based on the type specified.
* @param {number} length - The length of the OTP.
* @returns {string} - Generated OTP.
*/
// Generate Mixed OTP (Alphabets + Numbers)
const mixOtp = (length) => genOtp(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
// Generate Numeric OTP (Only Numbers)
const numOtp = (length) => genOtp(length, "0123456789");
// Generate Alphabetic OTP (Only Letters)
const alphaOtp = (length) => genOtp(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
// Utility function to generate OTP from a given character set
const genOtp = (length, chars) => Array.from({ length }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
// Export the functions
module.exports = { mixOtp, numOtp, alphaOtp };