One-time Passwords (OTP) is a password that is valid for only one login session or transaction in a computer or a digital device. Now a days OTP’s are used in almost every services like Internet Banking, online transactions etc. They are generally 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 Math library. This article describe how to generate OTP using JavaScript.
Used Function:
- Math.random(): This function returns any random number between 0 to 1.
- Math.floor(): It returns floor of any floating number to a integer value.
Using the above function pick random index of string array which contains all the possible candidates of a particular digit of the OTP.
Example 1: This example generates 4 digit Numeric OTP:
<script> // Function to generate OTP function generateOTP() { // Declare a digits variable // which stores all digits var digits = '0123456789'; let OTP = ''; for (let i = 0; i < 4; i++ ) { OTP += digits[Math.floor(Math.random() * 10)]; } return OTP; } document.write("OTP of 4 digits: ") document.write( generateOTP() ); </script> |
Output:
OTP of 4 digits: 2229
Example 2: This example generates 6 digit Numeric OTP:
<script> // Function to generate OTP function generateOTP() { // Declare a digits variable // which stores all digits var digits = '0123456789'; let OTP = ''; for (let i = 0; i < 6; i++ ) { OTP += digits[Math.floor(Math.random() * 10)]; } return OTP; } document.write("OTP of 4 digits: ") document.write( generateOTP() ); </script> |
Output:
OTP of 6 digits: 216664
Example 3: This example generates alphanumeric OTP of lenght 6:
<script> // Function to generate OTP function generateOTP() { // Declare a string variable // which stores all string var string = '0123456789abcdefghijklmnopqrs tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; let OTP = ''; // Find the length of string var len = string.length; for (let i = 0; i < 6; i++ ) { OTP += string[Math.floor(Math.random() * len)]; } return OTP; } document.write("OTP of 4 lenght: ") document.write( generateOTP() ); </script> |
Output:
OTP of 6 lenght: rab0Tj
Recommended Posts:
- Password Matching using JavaScript
- Show and hide password using JavaScript
- JavaScript to generate random hex codes of color
- Program to generate random string in PHP
- Program to generate random alphabets
- Javascript | Program to read text File
- How to store a password in database?
- JavaScript | Program to write data in a text File
- MySQL | Change User Password
- Node JS | Password Hashing with Crypto module
- Generate number with given operation and check if it is palindrome
- How to generate a random, unique, alphanumeric string in PHP
- Generate random String of given size in Java
- Map in JavaScript
- this in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



