Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Program to generate one-time password (OTP)

  • Difficulty Level : Basic
  • Last Updated : 08 Dec, 2020
Geek Week

One-time Passwords (OTP) is a password that is valid for only one login session or transaction on a computer or a digital device. Nowadays OTP’s 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.

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 a 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 6 digits: ")
document.write( generateOTP() );
</script>                    

Output:

OTP of 6 digits: 216664

Example 3: This example generates alphanumeric OTP of length 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 6 length: ")
document.write( generateOTP() );
</script>                    

Output:

OTP of 6 length: rab0Tj

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :