Jump to content

Form validation help


amg182

Recommended Posts

Hi guys, any help with this would be much appreciated.

 

I have some JS validating my form to check that the fields are not empty/balnk- if they are it will highlight the fields (using css classes). It is working fine but only highlights one empty field at a time. Is it possible to get it to highlight all the fields that are empty/blank?

 

For example. If the user forgets to fill in 2 fields, it will highlight one field at a time. If the user then inputs something to that field and tries to submit it will then highlight the next filed. Instead, is it possible to have it highlight ALL empty fields?

 

function validateForm()
{

var x=document.forms["advert"]["name"].value;
if (x=="")
  {
  //changes class of li_25 to error class in order to highlight field
  document.getElementById("li_25").className = "error";
  return false;
  }
  

  var x=document.forms["advert"]["email"].value;
if (x=="")
  {
  document.getElementById("li_36").className = "error";
  return false;
  }
  
    var x=document.forms["advert"]["age"].value;
if (x=="")
  {
  document.getElementById("li_17").className = "error";
  return false;
  }

  }

 

Thanks guys

Link to comment
Share on other sites

You can add a variable on top of your function to hold the valid status and check all the fields. If one is invalid, set the variable to false. In the end of the function, return that variable.

e.g.

function validateForm(){
var is_valid = true;
...
if (x=="")  {
    document.getElementById("li_25").className = "error";
    is_valid = false;
}
...
return is_valid;
}

Link to comment
Share on other sites

hi again.

 

How would i go about placing one alert messagee if one or more fields are  left blank/empty?

i.e. A message saying "Please fill in ALL fields" I can manage to do it for each input field but obiviously not practical if there are 5+ fields left blank otherwise the user is bombarded wiht alert messages!

 

Thanks again

Link to comment
Share on other sites

You would add an array to hold the error messages and in the end alert that array

e.g.

function validateForm(){
var err_arr = [];
...if (x=="")  {
document.getElementById("li_25").className = "error";
err_arr.push(' - field name');
}
...
if (err_arr.length){
alert('Please complete the following:\n'+err_arr.join('\n'));
return false;
}
return true;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.