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

140
Visualizações
How To Validate Form Field Using Javascript

I am trying to validate my form phone field to prevent users from entering the same numbers in my javascript.

If the number provided by the user matches with the same numbers in my javascript, They will get a warning and the form would not submit.

However, I noticed that my code below shows the warning whether the numbers match or not.

I need corrections to know what I am doing wrong. Thanks.

Code below;

 $('.validate').hide();
    $('body').on('blur', '#phone', function() {
        $('.validate').hide();
        isphone($(this).val());
    });

    function isphone(phone) {
        if (phone === "1234" || phone === "23456"){
            $(".validate").show();
         
        } else {
            $(".validate").hide();
        }
    }
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<form action='' method='POST' id="submitForm" >
   
 <input type="phone" name='phone'  required=''  id="phone" placeholder="0000-000-0000"/>

 <div class="validate"><span style="color: red;"><b>Please enter a valid phone!</b></span></div>
 
 <button href='/' type='submit' id="submitForm">Process</button>
 
 </form>

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

0

I believe this should work. I move the changes for hiding and showing $('.validate') to the isphone function, and removed that done variable that wasn't doing anything useful (used if...else instead). Also, don't use document.getElementById if you're already using JQuery.

    $('.validate').hide();
    $('body').on('blur', '#phone', function() {
        $('.validate').hide();
        isphone($(this).val());
    });

    function isphone(phone) {
        if (phone === "1234" || phone === "23456"){
            $(".validate").hide();
        } else {
            $(".validate").show();
        }
    }
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