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

154
Visualizações
using HTML variables with JavaScript

I'm creating a sign up system and tying to make a code using JavaScript that checks the phone number the user is entering and only allows it to be used if it is 10 characters long.

this is my code so far:

JavaScript:

<script type="text/javascript">
        function phoneIsValid()
        {
            var phoneL = document.getElementById("phone").length;
            if (phoneL != 10)
            {
                document.getElementById("phoneErr").innerHTML = "invalid phone number"
                return false;
            }
            else
            {
                document.getElementById("phoneErr").innerHTML = "";
                return true;
            }

        }
    </script>

HTML:

<input type="text" id="phone" name="phone" />
            <a id="phoneErr"></a>

the problem is that it always replys that the phone number is not 10 cahracters long, even if it is. what am I doing wrong? I dont know much Javascript and I cant find a solution on the internet.

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

0

You don't even need JS for this:

<form>
  <input type="text" minlength="10" maxlength="10" required placeholder="Phone number (10 digits)" />
  <button>Send</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

function phoneIsValid()
{
    var phoneL = document.getElementById("phone").value.length;
    if (phoneL != 10)
    {
        document.getElementById("phoneErr").innerHTML = "invalid phone number"
        return false;
    }
    else
    {
        document.getElementById("phoneErr").innerHTML = "";
        return true;
    }
}
function checkPhone() {
  if(phoneIsValid()) {
    alert('Phone number is valid');
  } else {
    alert('Phone number is NOT valid');
  }
}
<input type="text" id="phone" name="phone" />
            <a id="phoneErr"></a>
            
<button type="button" onclick="checkPhone()">Check it</button>

As mentioned in comments - you need to validate value of the textbox, not textbox itself.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try getting the length of the "value".

var phoneL = document.getElementById("phone").value.length;
                                                ˄
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