Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight required fields in variable

Status
Not open for further replies.

fergy1

IS-IT--Management
Jul 25, 2003
32
US
Hello-

I had posted something like this prior. However the iteration for each field to be highlighted wont work as in the code it only highlights the first field that was required. How can I put each element into a variable and have the form highlight all of the fields that were required at once?

Code:
if (theForm.pr_ny_estimate.value.length < 1)
  {
    themessage = themessage + " -  Payroll Next year estimate";
    theForm.pr_ny_estimate.style.backgroundColor = "#ffff00";
  }
if (theForm.pr_ly_estimate.value.length < 1)
  {
    themessage = themessage + " -  Payroll Last year estimate";
    theForm.pr_ly_estimate.style.backgroundColor = "#ffff00";
  }
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
   }
}
 
I don't understand... the code you've shown should run both "if" statements, and highlight both fields if required. Is this not what you're seeing? If so, can you post a cut-down test harness (complete), or a URL to the code, as it may well be that something else is causing only the first field to highlight.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Here is an example simply for testing purposes.

Code:
<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.first.value=="") {
themessage = themessage + " - First Name";
document.form.first.style.backgroundColor = "#ffff00";
}
if (document.form.last.value=="") {
themessage = themessage + " -  Last Name";
document.form.last.style.backgroundColor = "#ffff00";
}
if (document.form.email.value=="") {
themessage = themessage + " -  E-mail";
document.form.email.style.backgroundColor = "#ffff00";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
   }
}
//  End -->
</script>

</HEAD>

<BODY>

<form  name=form method="post" action="">
<input type=text name="first" size="20"> First Name<BR>
<input type=text name="last" size="20"> Last Name<BR>
<input type=text name="email" size="20"> E-Mail<BR><BR>
<input type=button value="Submit Request" onclick="verify();">
  
<input type=reset value="Clear Form"><br>
</form>

<p><center>
</center><p>
 
Incidentally, you should unhighlight all fields that do validate, otherwise if someone corrects a field, it will remain highlighted, giving the user the wrong impression.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Guys. My bigger file was checking for 3 different variables not just a value of x. This switched the color for me and stopped on each one as I had a different alert for that. The comment from Dan is good as well.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top