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

214
Visualizações
How can i make a text input to start the number 9?

I need a textbox to start with the number 9, how do i do it? The textbox only accepts numbers but i need it to start with the number 9. I've tried it like this but it doesn't work.

$(document).ready(function () { 
            var $phone = $("#phone");
            $phone.mask('900000000', {reverse: false});
        });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$(function(){
   //get the input element to const
   const $phn = $('#phone');
   //add keydown event
   //if value is a length of 1 and code is backspace or delete return false
   //if length is longer than 1 and delete key is pressed, reset to just 9
   $phn.on('keydown', function(e){
       const code = e.which || e.keyCode;
       //keycode 8 = backspace, keycode 46 = delete
       if($phn.val().length === 1 && (code === 8 || code === 46)){
          return false;
       }
       else if(code === 46){
          $phn.val('9');
          placeCursor($phn);
       }
   });
   
   //initialize value to 9 and place cursor
   $phn.val('9');
   placeCursor($phn);
   
   //function to focus and place cursor in input box
   function placeCursor(input){
      $(input)[0].focus();
      $(input)[0].setSelectionRange($(input).val().length, $(input).val().length);
   }
});
<input type="text" id="phone" />
<script
  src="https://code.jquery.com/jquery-3.6.0.slim.min.js"
  integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI="
  crossorigin="anonymous"></script>

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