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

202
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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