Skip to content
The Chat Archive
gemma-3-27b-it· Google LMArena Human Preference 140K

I am a hiring man from IT company and try to test new frontend programmer. You are at…

  1. User

    I am a hiring man from IT company and try to test new frontend programmer. You are at your test day and achieve task to present 6 fields in html/css/js coding for OTP. Inputs should be able to take only one digit after typing it focus to the next field. They should be one by one in a line and be easy to cope with. Write html,css and js for it. Present it to me in those three blocks of code and show me the finall result I could test if it works. Good luck!

  2. Assistant

    Okay, here's the HTML, CSS, and JavaScript code for a six-field OTP input, designed to automatically focus on the next field after a single digit is entered. I've included explanations and comments to make it clear. I've also included a final, complete HTML file you can copy and paste to test directly.

    HTML (index.html)

    <!DOCTYPE html>
    <html>
    <head>
      <title>OTP Input</title>
      <link rel="stylesheet" href="style.css">
    </head>
    <body>
    
      <div class="otp-container">
        <input type="text" id="otp1" maxlength="1" class="otp-input">
        <input type="text" id="otp2" maxlength="1" class="otp-input">
        <input type="text" id="otp3" maxlength="1" class="otp-input">
        <input type="text" id="otp4" maxlength="1" class="otp-input">
        <input type="text" id="otp5" maxlength="1" class="otp-input">
        <input type="text" id="otp6" maxlength="1" class="otp-input">
      </div>
    
      <script src="script.js"></script>
    </body>
    </html>
    

    Explanation:

    • <!DOCTYPE html>: Declares the document type as HTML5.
    • <title>: Sets the title of the page.
    • <link rel="stylesheet" href="style.css">: Links the external CSS file for styling.
    • <div class="otp-container">: A container to hold all the input fields. This helps with layout and styling.
    • <input type="text" ...>: Each input field represents a single digit of the OTP.
      • id="otp1", id="otp2", etc.: Unique IDs for each input field, used by the JavaScript to reference them.
      • maxlength="1": Limits the input to a single character. This is crucial for the auto-focus behavior.
      • class="otp-input": A class for applying common styling to all input fields.
    • <script src="script.js"></script>: Links the external JavaScript file for the logic.

    CSS (style.css)

    .otp-container {
      display: flex;
      justify-content: space-around; /* Distribute inputs evenly */
      align-items: center;
      margin: 20px;
    }
    
    .otp-input {
      width: 40px;
      height: 40px;
      text-align: center;
      font-size: 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 5px;
    }
    
    .otp-input:focus {
      outline: none;
      border-color: #007bff; /* Highlight on focus */
      box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
    }
    

    Explanation:

    • .otp-container:
      • display: flex;: Uses flexbox for easy horizontal arrangement of the input fields.
      • justify-content: space-around;: Distributes the input fields evenly with space around them.
      • align-items: center;: Vertically centers the input fields within the container.
      • margin: 20px;: Adds some margin around the container.
    • .otp-input:
      • width: 40px;, height: 40px;: Sets the size of each input field.
      • text-align: center;: Centers the text within the input field.
      • font-size: 20px;: Sets the font size.
      • border: 1px solid #ccc;, border-radius: 5px;: Adds a border and rounded corners.
      • padding: 5px;: Adds some padding inside the input field.
    • .otp-input:focus:
      • outline: none;: Removes the default focus outline.
      • border-color: #007bff;: Changes the border color when the input field is focused.
      • box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);: Adds a subtle box shadow on focus.

    JavaScript (script.js)

    const inputs = document.querySelectorAll('.otp-input');
    
    inputs.forEach((input, index) => {
      input.addEventListener('input', function() {
        if (this.value.length === 1) {
          if (index < inputs.length - 1) {
            inputs[index + 1].focus();
          } else {
            // If it's the last input, you can do something else, like submit the form
            // For example:
            // document.getElementById('otpForm').submit();
          }
        } else if (this.value.length > 1) {
          this.value = this.value.slice(0, 1); // Limit to one character
        }
      });
    
      // Allow backspace on the first input to clear all fields
      if (index === 0) {
        input.addEventListener('keydown', function(event) {
          if (event.key === 'Backspace' && this.value === '') {
            for (let i = inputs.length - 1; i >= 0; i--) {
              inputs[i].value = '';
              if (i > 0) {
                inputs[i - 1].focus();
              }
            }
          }
        });
      }
    });
    

    Explanation:

    • const inputs = document.querySelectorAll('.otp-input');: Selects all elements with the class otp-input and stores them in a NodeList called inputs.
    • inputs.forEach((input, index) => { ... });: Iterates over each input field in the inputs NodeList.
    • input.addEventListener('input', function() { ... });: Adds an event listener to each input field that listens for the input event (triggered whenever the input value changes).
    • if (this.value.length === 1) { ... }: Checks if the input field now contains exactly one character.
      • if (index < inputs.length - 1) { ... }: If it's not the last input field, focus on the next input field.
      • inputs[index + 1].focus();: Sets the focus to the next input field.
      • else { ... }: If it's the last input field, you can add code to handle the complete OTP (e.g., submit a form).
    • else if (this.value.length > 1) { ... }: If the input field contains more than one character, truncate it to one character.
      • this.value = this.value.slice(0, 1);: Extracts the first character of the input value.
    • Backspace Handling (index === 0):
      • The code adds a keydown event listener to the first input field (index === 0).
      • It checks if the user pressed the Backspace key and the input field is empty.
      • If both conditions are true, it clears all the input fields, starting from the last one and moving backwards, and focuses on the previous input field.

    Complete HTML File (index.html - for easy testing)

    <!DOCTYPE html>
    <html>
    <head>
      <title>OTP Input</title>
      <style>
        .otp-container {
          display: flex;
          justify-content: space-around;
          align-items: center;
          margin: 20px;
        }
    
        .otp-input {
          width: 40px;
          height: 40px;
          text-align: center;
          font-size: 20px;
          border: 1px solid #ccc;
          border-radius: 5px;
          padding: 5px;
        }
    
        .otp-input:focus {
          outline: none;
          border-color: #007bff;
          box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
        }
      </style>
    </head>
    <body>
    
      <div class="otp-container">
        <input type="text" id="otp1" maxlength="1" class="otp-input">
        <input type="text" id="otp2" maxlength="1" class="otp-input">
        <input type="text" id="otp3" maxlength="1" class="otp-input">
        <input type="text" id="otp4" maxlength="1" class="otp-input">
        <input type="text" id="otp5" maxlength="1" class="otp-input">
        <input type="text" id="otp6" maxlength="1" class="otp-input">
      </div>
    
      <script>
        const inputs = document.querySelectorAll('.otp-input');
    
        inputs.forEach((input, index) => {
          input.addEventListener('input', function() {
            if (this.value.length === 1) {
              if (index < inputs.length - 1) {
                inputs[index + 1].focus();
              } else {
                // If it's the last input, you can do something else, like submit the form
                // For example:
                // document.getElementById('otpForm').submit();
              }
            } else if (this.value.length > 1) {
              this.value = this.value.slice(0, 1); // Limit to one character
            }
          });
    
          // Allow backspace on the first input to clear all fields
          if (index === 0) {
            input.addEventListener('keydown', function(event) {
              if (event.key === 'Backspace' && this.value === '') {
                for (let i = inputs.length - 1; i >= 0; i--) {
                  inputs[i].value = '';
                  if (i > 0) {
                    inputs[i - 1].focus();
                  }
                }
              }
            });
          }
        });
      </script>
    </body>
    </html>
    

    How to Test:

    1. Save the complete HTML file (above) as index.html.
    2. Create a file named style.css and paste the CSS code into it.
    3. Create a file named script.js and paste the JavaScript code into it.
    4. Open index.html in your web browser.

    You should now have a six-field OTP input. Type a single digit into the first field, and it should automatically move the focus to the next field. You can use the backspace key in the first field to clear all the fields.

    This provides a solid, functional OTP input component. Let me know if you'd like any modifications or enhancements!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.