RE: form validation

by "Guy M. Fisher" <guyfisher(at)email.com>

 Date:  Sat, 22 Dec 2001 14:35:11 -0500
 To:  "'Roger Harness'" <magic32(at)jps.net>,
<hwg-techniques(at)mail.hwg.org>
 In-Reply-To: 
  todo: View Thread, Original
>> I tried to get it to check for a selected radio button (the last if
statement). I'm sure my syntax isn't correct (obviously), but i've tried
every combination to check for that empty address field AND for a "yes"
checked radio button. <<

I'm not a JavaScript expert either, Roger, but I think I see what's wrong.
When you have a set of radio buttons with the same <name> attribute it
creates an array of form elements instead of a single form element. You
need to loop through the array to find out which element is checked and
then retrieve its value.

So, your radio buttons create an array of form elements named
"want_newsletter:"

	<input type="radio" name="want_newsletter" value="yes" checked>yes
	<input type="radio" name="want_newsletter" value="no">no

To find out which button was selected, add a loop to the isReady() function
that goes through the want_newsletter array until you find the element
whose checked property is "true" and assign that element's value property
to a variable:

	for (i = 0; i < form.want_newsletter.length; i++) {

		if (form.want_newsletter[i].checked) {

			var news = form.want_newsletter[i].value;

		}

	}

Finally, use the variable from the previous loop in the last if statement
of your isReady() function instead of the form.want_newsletter.checked
property:

	if (isFilled(form.address) == false && news == "yes") {

		alert("please enter your address to receive newsletter");
		form.address.focus();
		return false;

	}


Hope that helps.


Guy M. Fisher

Cleveland, Ohio
guyfisher(at)email.com

guyfisher.com | Hand-Crafted Bookmarklets
http://www.guyfisher.com/builder/bookmarklets/

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA

This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.