[thelist] js validation suggestions
Scott Brady
evolt at scottbrady.net
Mon Feb 10 09:13:00 CST 2003
From: "Tom Dell'Aringa" <pixelmech at yahoo.com>
>I'm thinking there might be a better way to validate this instead of
>doing:
>
>if((field == "") && (field == "") && (field == "") etc ad nauseum..)
One thing you can do is loop over the form fields. Something like this:
-------- Begin code -----------
function validate(theForm)
{
var filledYN = false;
for (i=0; i<theForm.length; i++)
{
if ((theForm[i].type == 'text') && (theForm[i].value.length > 0))
{
filledYN = true;
}
else if ((theForm[i].type == 'select') && (theForm[i].selectedIndex >= 0))
{
filledYN = true;
}
}
if (!filledYN)
{
alert('Please select at least one criterion.');
}
return filledYN;
}
-------- End code -----------
Oh, here's the form definition:
<form name="thisForm" action="whatever" method="post" onsubmit="return validate(this);">
That should do the trick. I'm writing this off the top of my head, so you might want to verify that a selectbox does return a "type" of "select".
Hope that helps.
Scott Brady
More information about the thelist
mailing list