Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

367
Vistas
How to use input mask in Javascript for Mac ID & Phone Number

I want to filter Phone numbers & Mac id from one input field... if the string starts with '00' then put a colon after every two integers with characters & if it starts with any other number did not put a colon after two numbers...

var dynamicMask = new IMask(document.getElementById('user'), {
    mask: [{
      mask: '0*:**:**:**:**:**'
    },
    {
      mask: /^\S*@?\S*$/
    }
  ]
})


<script src="https://unpkg.com/imask"></script>
<div class="form-group">
  <label>Mac id or phone</label>
  <input type="text" class="form-control" name="user" id="user" placeholder="Enter ..." />
</div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's a bit tricky to allow multiple formats depending on the values. Phone numbers have no real standard (except the starting numbers of 00 or + for international, 0 for national and 1-9 for provider/location at the beginning) and could also be of length 12 and they hardly depend on the country/provider/location of the target.

I suggest using two different input fields instead of two formations depending on the input length/value.

But something like this will do it:

<input type="text" id="user">
<script>
  /*
   * This allows:
   
   @0123456789af,  @01:23:45:67:89:af and 01:23:45:67:89:af 
      threaded as mac address
   
   any other input like:
   00:00:00:00:00 or 01:23:45:67:89:az
     handled as phone number
     (removes any non numeric value on focusout)
 */
  IMask(user, {
    mask: /[@:0-9a-f]/gi, // allow anything
    prepare(char) {
      // allow only @, :, 0-9 and a-f for input
      // this allows: @[a-f0-9]{12} values to be entered or 00:00:00:00:00:00...
      return char.replace(/[^@:0-9a-f]/i, '');
    },
    commit(value, masked) {
      // leading @ will be removed
      // check if it contains already a : and has a length of 6 sets with 2 byte length each
      if (value.split(':').length == 6 && value.split(':').every(p => p.length == 2) && !value.startsWith('@')) {
        // add an @ leading for the text below
        value = '@' + value;
      }
      value = value.replace(/:/g, '');
      const dummy = document.createElement("input");
      // required else call for IMask() on dummy won't work
      document.body.appendChild(dummy);
      dummy.setAttribute("value", value.startsWith('@') ? value.substr(1) : value);
      // length must be 12 and should not start with 00
      if (/^@[0-9a-f]{12}$/i.test(value)) {
        // remove the leading @ that was added before or by the user and add : formation
        masked._value = IMask(dummy, {
          mask: '**:**:**:**:**:**'
        })._value;
      } else {
        // filter only numbers
        masked._value = IMask(dummy, {
          mask: /.*/,
          prepare(str) {
            return str.replace(/[^0-9]/, '');
          }
        })._value;
      }
      dummy.remove();
    }
  });
</script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda