Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Using array in Javascript

I'm using a jQuery plugin to build an input mask, but my code is too large.

The user must not be able to start the phone number with zero. The error message has to disappear when he types the right number.

How can I optimize this code? Can I use an array for the numbers? I haven't found a way to do this. Thanks in advance!

var inputMask = function(val) {
  return '(00) 0 0000-0000'
},
noZero = {
  onKeyPress: function(val, e, field, options) {
    if (field.val() == "") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(1") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(2") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(3") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(4") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(5") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(6") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(7") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(8") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(9") {
      document.getElementById("PhoneError").innerHTML = "";
    } else if (field.val() == "(0") {
      document.getElementById("PhoneError").innerHTML = "Don't start with zero";
      field.val(field.val().substr(0, 1))
    }
    field.mask(inputMask.apply({}, arguments), options)
  }
};
  
$("input[type='tel']").mask(inputMask, noZero)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>
<input type="tel" id="tel" placeholder="tel">
<div id="PhoneError" style="color:red"></div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're evaluating all states, both good and bad, instead of just checking for the invalid states of the value.

You can simply clear the error message when a new key is pressed, then check the value for known invalid rules; in this case, beginning with (0.

let $phoneError = $('#PhoneError');
let inputMask = function(val) {
  return '(00) 0 0000-0000'
};
let noZero = {
  onKeyPress: function(val, e, field, options) {
    $phoneError.text(''); // clear the error when a new key is typed.
    
    // evaluate known invalid states only:
    if (val == "(0") {
      $phoneError.text("Don't start with zero");
      field.val((i, v) => v.substr(0, 1));
    }
    field.mask(inputMask.apply({}, arguments), options)
  }
};
  
$("input[type='tel']").mask(inputMask, noZero)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>
<input type="tel" id="tel" placeholder="tel">
<div id="PhoneError" style="color:red"></div>

As an aside to the issue, returning a hard-coded string from a function to set inputMask is not necessary. You can just set the string directly:

let inputMask = '(00) 0 0000-0000';
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda