[thelist] js validation suggestions

Tom Dell'Aringa pixelmech at yahoo.com
Mon Feb 10 09:55:01 CST 2003


--- Scott Brady <evolt at scottbrady.net> wrote:
> 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".

Excellent Scott! Just what I was looking for..why didn't I think of
that..

Tom

=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the thelist mailing list