Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!