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

196
Vistas
How to edit my function to return an error if password isn't 8 characters?

This is my javascript but it works fine with all the other fields when nothing is entered I'm trying to make the user need to enter at least 8 characters before submitting but currently, they can submit on 0.

function validateForm() {
    if (document.register.firstName.value == "") {
        alert("Please provide your first name!");
        document.register.firstName.focus();
        return false;
    }
    if (document.register.secondName.value == "") {
        alert("Please provide your second name!");
        document.register.secondName.focus();
        return false;
    }
    if (document.register.email.value == "") {
        alert("Please provide your Email!");
        document.register.email.focus();
        return false;
    }
    if (document.register.phoneNo.value == "") {
        alert("Please provide your Phone Number!");
        document.register.phoneNo.focus();
        return false;
    }
    if (document.register.Gender.value == "") {
        alert("Must select a Gender!");
        document.register.Gender.focus();
        return false;
    }
    if (document.register.Terms.value == "") {
        alert("Terms and Conditions must be accpeted");
        document.register.Terms.focus();
        return false;
    }
    if (document.register.psw.value.length > 7) {
        alert("Password must contain 8 character");
        document.register.psw.focus();
        return false;
    }
    return (true);
}
about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

You need to check if it's <= 7 characters in length (or probably < 8 for clarity). Currently you're preventing the user from submitting a password if it's too long, not the other way around:

if (document.register.psw.value.length < 8) { 
    ...
}
about 4 years ago · Santiago Gelvez Denunciar

0

I'm assuming you're using the password input field. If so, you can handle that from the HTML by doing something like this (see below) and have the browser handle the validation.

<input type="password" minlength="4" maxlength="8">
about 4 years ago · Santiago Gelvez Denunciar

0

Just try the following. Notice, as you mentioned, it is not greater than 7, but less than 8.

if( document.register.psw.value.length < 8) {
    alert( "Password must contain 8 character" );
    document.register.psw.focus() ;
    return false;
}  
about 4 years ago · Santiago Gelvez 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