Getting Started with EJ2 JavaScript OTP Input

21 Nov 20263 minutes to read

This section explains the steps required to create a simple Essential® JS 2 OTP input and demonstrate the basic usage of the OTP input control in a JavaScript application.

Dependencies

The list of dependencies required to use the OTP input component in your application is given below:

|-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-base

Setup for local environment

Refer to the following steps to set up your local environment.

Step 1: Create a root folder named my-app for your application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 OTP input control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and Scripts

Add Syncfusion® OTP Input CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base scripts are required dependency packages for the OTP Input component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/otp-input/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.js

Adding OTP Input component to the application

Add the OTP Input element and initiate the JavaScript (ES5) OTP Input control in the index.html by using following code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"></script>
  <title>JavaScript (ES5)</title>
  <!-- JavaScript (ES5) OTP Input's dependency style -->
  <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/otp-input/index.css" rel="stylesheet" />

  <!-- JavaScript (ES5) OTP Input's dependency global script -->
  <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>

  <!-- JavaScript (ES5) OTP Input's control global script -->
  <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>

  <!--style reference from app-->
  <link href="index.css" rel="stylesheet" />
</head>

<body>
  <!--Element to render the OTP Input control-->
  <div id='container' style="width: 350px;">
    <div id="otp_default"></div>
  </div>
  <script>
    // Initializes the OTP Input control
    var otpInput = new ej.inputs.OtpInput({});

    // Render initialized OTP Input.
    otpInput.appendTo('#otp_default');
  </script>
  <script>
    var ele = document.getElementById('container');
    if (ele) {
      ele.style.visibility = "visible";
    }   
  </script>
</body>

</html>
#container {
  visibility: hidden;
  margin: 50px auto;
  width: 350px;
}

#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}