Program to generate one-time password (OTP) with Special Characters
In this method, we are only generating OTP with Special characters. We are storing all the digits(0 to 9) in a variable called digits and by the use of the functions Math.floor() and Math.random in a for loop, we are generating the 6-digit Alphanumeric having special characters OTP.
Example: This example generates Alphanumeric having special characters OTP of length 6.
javascript
// Function to generate OTP function generateOTP() { // Declare a digits variable // which stores all digits let digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+' ; let OTP = '' ; let len = digits.length; for (let i = 0; i < 6; i++) { OTP += digits[Math.floor(Math.random() * len)]; } return OTP; } console.log( "OTP of 6 digits: " ) console.log(generateOTP()); |
OTP of 6 digits: 052581
JavaScript Program to generate one-time password (OTP)
A one-time Password (OTP) is a password that is valid for only one login session or transaction on a computer or a digital device. Nowadays they are used in almost every service like Internet Banking, online transactions, etc. They are generally a combination of 4 or 6 numeric digits or a 6-digit alphanumeric. The random function is used to generate random OTP which is predefined in the Math library. This article describes how to generate OTP using JavaScript.