Robby Morales
Robby Morales

Reputation: 39

IF/ else statements and Operators

I working on a form but I have some troubles with the last if statement ( it's not working.

count is global and start as 0, for each field with the correct character filled in this will happen: count = count+1;

but if I clicked submit and leave 2 fields with not the correct character

( that should be count = 6 ) it doesn't give me an alert but skips it

this is how it should be.

  1. check if password equals confirm_password
  2. check if fields or not empty
  3. check if fields have the correct characters ( count start as count = 0 , foreach field that is correct it it goes count = count +1, in totaal it can get 8 but at 6 it still keeps submitting).

function validateForm() {

  var fields = ["voornaam", "achternaam", "Email", "Wachtwoord", "Herhaal_Wachtwoord", "Straatnaam", "Huisnummer", "Postcode", "Woonplaats", "Telefoonummer"];

  if (pass1.value !== pass2.value) {
    alert("Wachtwoord komen niet overeen");
    return false;
  }
  
  var l = fields.length;
  var fieldname;
  for (i = 0; i < l; i++) {
    fieldname = fields[i];
    if (document.forms["register"][fieldname].value === "") {
      alert(fieldname + " can not be empty");
      return false;
    }
  }

  if (count < 8) {
    alert("iets is niet goed ingevuld");
    return false;
  }
}

Upvotes: 0

Views: 93

Answers (1)

toadflax
toadflax

Reputation: 375

You have returned the false boolean before you have given the alert prompt! Just change the last if statement like this:

if (count < 8) {
    alert("iets is niet goed ingevuld");
    return false;
}

Upvotes: 1

Related Questions