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

226
Visualizações
Jquery to allow negative and positive numbers on text field and negative sign should have 0 indexes

I need to validate a text field to accept only negative or positive numbers and indexing also matters. this is what I have tried so far.

this is HTML code

<input type="text" class="negativeaccept" >

here is Jquery

   $(document).on('keypress','.negativeaccept', function(event) {
   if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) ||
        $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
    event.preventDefault();
   }  
   });

this code only accepts a positive number with a decimal. I want a negative sign (-) of The 0 indexes.

i try this code and its working (event.which != 45 || $(this).val().indexOf('-') != -1) && but negative (-)sign enter any index. can you try this better than this a solution.

$(document).on('keypress','.negativeaccept', function(event) {
    if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) ||
            $(this).val().indexOf('.') != -1) && (event.which != 45 || $(this).val().indexOf('-') != -1) &&
             (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="negativeaccept">

about 4 years ago · Juan Pablo Isaza
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