JSFiddle - React, Tailwind, and code Playground

by khushboo097

HTML

<input type="text" id="pwd" maxlength="11" onkeyup="myFunction()">
<button onclick="myFunction(true)">save</button>

JavaScript

var numberEntered = '';

function myFunction(isSave) {
  var x = document.getElementById("pwd");
  if (isSave || x.value.length>11) {
    alert('your number is: '+Number(numberEntered)); //save your number here.
    return;
  }
  
  numberEntered = numberEntered.concat(x.value.charAt(x.value.length - 1));
  
  var n = x.value.length;
  if (n == 3 || n == 6) {
    x.value = x.value.slice(0, x.value.length - 1) +
      "X";
    x.value = x.value.concat('-');
    return;
  }
  x.value = x.value.slice(0, x.value.length - 1) +
    "X";
}