Click to See Complete Forum and Search --> : Script improvement for checking for blank fields


florida
01-28-2003, 10:58 AM
I have a script that checks for blank entries in my form. It shows one alert message at a time if all three fields are blank. So I would get three seperate alert messages if I had three blanks or get two seperate alert messages if I had two blank fields. How can I get it where pops up message with ONE alert message listing ALL three blank values saying "Fieldname, Fieldname2, Fieldname3 have blank values" or if two values are blank it would say "Fieldname, Fieldname3 have blank values"

Here is script that works but just pops up one alert message at a time for each blank.


<script>
function checkBlanks()
{
if(document.myform.fieldname.value == "")
{
alert("Enter fieldname information.");
return false
}
else if(document.myform.fieldname2.value == "")
{
alert("Enter fieldname2 Information.");
return false
}
else if(document.myform.fieldname3.value == "")
{
alert("Enter fieldname3 information.");
return false
}

}
</script>

<form name="myform" action="myformadd" onSubmit="return checkBlanks();">

gil davis
01-28-2003, 12:44 PM
<script>
function checkBlanks() {
var errs = "";
if(document.myform.fieldname.value == "")
{errs += "Enter fieldname information.\n";}
if(document.myform.fieldname2.value == "")
{errs += "Enter fieldname2 Information.\n";}
if(document.myform.fieldname3.value == "")
{errs += "Enter fieldname3 information.\n";}
if (errs == "")
{return true}
else
{alert(errs);
return false;}
}
</script>

florida
01-29-2003, 05:38 AM
thanks, anyway to make this into a for loop as I have about 15 fields I need to enter or should I just do this 15 times?

if(document.myform.fieldname.value == "")
{errs += "Enter fieldname information.\n";}
if(document.myform.fieldname2.value == "")
{errs += "Enter fieldname2 Information.\n";}
if(document.myform.fieldname3.value == "")
{errs += "Enter fieldname3 information.\n";}