Re: Form validation

by "Martin Clifford" <MLC1(at)nrc.gov>

 Date:  Thu, 30 May 2002 13:27:04 -0400
 To:  <amcbainezzell(at)alum.mit.edu>,
<hwg-techniques(at)hwg.org>
  todo: View Thread, Original
Personally, I would combine the two.

Have JavaScript check to see which button is selected.  If the "Other" =
button is selected, set the focus() to the text field.  If one of the =
other buttons is selected, clear the text field ONLY when the user hits =
the submit button.  If you clear the text field when they select another =
button, the user may change their mind and have to retype in the informatio=
n.  Then have JS do the usual validation of the other form elements.

<input type=3D"button" value=3D"Go!" name=3D"submitButton" onClick=3D"retur=
n validateForm(this);">

function validateForm(form) {

// Check for button status
if(form.otherButton !=3D checked) {
	form.textField.value =3D '';
	return true;
	} else if(form.otherButton =3D=3D checked && form.textField.value =
=3D=3D '') {
		alert('Please enter Other information.');
		return false;
	} else if(form.otherButton =3D=3D checked && form.textField.value =
!=3D '') {
	return true;
	}

//Other validations here
}

Something along those lines.

Hope this helps! :o)

Martin

>>> "Ann Ezzell" <amcbainezzell(at)alum.mit.edu> 05/30/02 11:35AM >>>

Which do you think is the better approach for client-side form
validation when the form has a set of radio buttons to select an option,
and one of the options is "other", and there is also a text box to
specify the "other"?

Option 1:

If the user types in the text box, the "other" radio button is
automatically selected. If the user selects a radio button that's not
"other", the "other" text box is cleared. Javascript is used as a backup
check before submitting the form.


Option 2:

Javascript is used to check the status of the answers before submitting
the form - the user is asked to correct his/her response if the form is
not completed correctly (e.g., there's text in the "other" text box, but
a non-other option is selected)

Other approaches?

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