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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


Output:



OTP of 4 digits: 2229

Example 2: This example generates 6 digit Numeric OTP:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


Output:

OTP of 6 digits: 216664

Example 3: This example generates alphanumeric OTP of lenght 6:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


Output:

OTP of 6 lenght: rab0Tj


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.