Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

Try getting the length of the "value".

var phoneL = document.getElementById("phone").value.length;
                                                ˄
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda