How to put Nextel mask in an <input> with Javascript?

Asked

Viewed 354 times

5

I’d like to put a mask on a <input> waiting for the format Nextel:

<input type="text" name="nextel">

The problem that according to the website the format of Nextel is this:

nextel formato

  • Urban: 2 digits
  • Fleet: 1 to 7 digits
  • ID: 2 to 5 digits

O be the Fleet and the ID has variant quantity numbers, I have used the plugin http://github.com/igorescobar/jQuery-Mask-Plugin, I do not know how to do for example from 1 to 7 for fleet.

Yet I seek solution with any other plugin or even in pure Javascript.

4 answers

3


You can put three numeric type fields side by side, it may not be the best solution but it is still an alternative.

Through the attributes min and max you can control how many digits the field can receive: if the maximum is two, then the user can enter any value between 10 and 99.

I see this as something good from the point of view of usability, after all if the field only accepts numbers nothing better than opening the numeric keyboard when the user access the site by mobile.

<div id='nextel'>
  <input type='number' min='10' max='99'>
  <input type='number' min='1'  max='9999999'>
  <input type='number' min='10' max='99999'>
</div>

As many as three fields, taking the value is no problem. Just one querySelectorAll('#nextel input') concatenating the value of each.


# snippet:

function getNextelNumber(){
  let nextel = '';
  for(let input of document.querySelectorAll('.nextel input'))
    nextel += input.value;
  return nextel;
}

document.querySelector('button').addEventListener('click', () => {
  alert(getNextelNumber());
});
.nextel input {
  border:none;
  border-bottom:1px solid #ccc;
  outline:none;
  width: 7ch
}

.nextel input:invalid {
  box-shadow: none;
  border-bottom-color: red
}

/* Removendo a aparência padrão do campo 'number'. */
input[type='number'] {
    -moz-appearance: textfield;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
<div class='nextel'>
  <input type='number' min='10' max='99'>
  <input type='number' min='1'  max='9999999'>
  <input type='number' min='10' max='99999'>
</div>

<br><br>
<button>Qual é meu Nextel?</button>

  • Note: This solution is not cross-browser (yet).

  • 1

    Can change the max for maxlength, I will not accept the answer yet because I hope to do in an input, however it is the solution I am using and therefore deserves +1 :) (and then I will launch a reward)

  • maxlength only works on type input text, email, search, password, tel or url :/ has to be in the gambit of the "maximum allowed value" same.

  • good to know, but it was due to the problem of not being cross-browser. For example it would be better to have maxlength at least, even if min does not work ;)

1

You can use the browser validator with the attribute pattern. This combined with a placeholder can be an elegant way to solve the problem.

<form action="demo_form.asp">
  Nextel: <input type="text" name="nextel" pattern="\d{2}[*]{1}\d{1,7}[*]{1}\d{2,5}" title="Insira um nextel valido" placeholder="ZZ*XXXXXXX*YYYYY">
  <input type="submit">
</form>

  • Beware if you put pattern does not work for Safari and other mobile devices. (see http://html5pattern.com/)

  • Thanks Bruno! But Pattern is not the same thing that masks.

  • @Guilhermenascimento Ah I noticed the mask I was referring to. Can you use the placeholder? Or you want something more explicit?

  • @Brunocosta is a good suggestion to combine placeholder+Pattern, but I’ll wait for other answers

0

I found another way to do this with the jquery input Mask. Unlike the plugin you mentioned, this allows you to click on the next field of the mask, from the moment the current one is valid.

 $('input[name=nextel]').inputmask("\d{2}-\d{1,7}-\d{2,5}"); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.1/jquery.inputmask.bundle.min.js"></script>

<form action="demo_form.asp">
  Nextel: <input type="text" name="nextel">
  <input type="submit">
</form>

jsfiddle

  • When using stacksnippet always add the libs together, if not the example does not work.

  • @Guilhermenascimento Maybe the diva didn’t use it, that’s why I added an Ink to jsfiddle.

  • Brunocost is preferred to keep the codes on the site, links can "break", if the stacksnippet already does the service actually use jsfiddle is unnecessary, I only use jsfiddle when I need some functionality that Stacksnippet does not support ;)

  • @Guilhermenascimento See what I think about this history of links breaking

  • I think you do not understand the problem goes beyond that and has been discussed several times in META, I even understand your point of view, but see, I am a visitor, I can test the code directly here on the site, because I still have to click to access an external resource? It’s not Stack Exchange that’s evangelizing something, it’s what users have been noticing since 2012, if you go to Soen you’ll notice several old broken links (usually responses from 2009 ~ 2012). The SE decided nothing, were a large part of the most participative users who noticed the problem ;)

0

Use plugin Vanillamasker is pure javascript without jquery

function inputHandler(masks, max, event) {
    var c = event.target;
    var v = c.value.replace(/\D/g, '');
    var m = c.value.length > max ? 1 : 0;
    VMasker(c).unMask();
    VMasker(c).maskPattern(masks[m]);
    c.value = VMasker.toPattern(v, masks[m]);
}

var telMask = ['(99) 9999-99999', '(99) 99999-9999'];
var tel = document.querySelector('input[attrname=telephone1]');
VMasker(tel).maskPattern(telMask[0]);
tel.addEventListener('input', inputHandler.bind(undefined, telMask, 14), false);
div {
  font-family: Arial;
  padding: 5px;
  margin-bottom: 10px;
}  
input {
  box-sizing: border-box;
  padding: 10px;
  border-radius: 5px;
  border-color: black;
  width: 250px;
  font-size: 1.3em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-masker/1.1.0/vanilla-masker.min.js"></script>
<div>
<label for="tel">Telefone</label>
<input attrname="telephone1" type="text">

  • Thank you, but I said Nextel and not phone format, this would be a mask for the phone associated with Nextel, what I want is a mask for the Urban*Frota*ID.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.