Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q360 Is it possible to validate both a pull down menu and a text area when a form is submitted?

You are here: irt.org | FAQ | JavaScript | Form | 3.4 | Q360 [ previous next ]

Yes, although there is always one item in a pulldown menu that is always selected, therefore make the first entry a comment type entry:

<SCRIPT LANGUAGE="JavaScript"><!--
function validate() {
    var valid = true, output = '';

    if (document.myForm.mySelect.selectedIndex == 0) {
        valid = false;
        output = 'Select an entry from the pulldown menu\n';
    }

    if (document.myForm.myText.value.length == 0) {
        valid = false;
        output += 'Enter some text in the text area';
    }

    if (!valid)
        alert(output);

    return valid;
}
//--></SCRIPT>

<FORM NAME="myForm" onSubmit="return validate()">
<SELECT NAME="mySelect">
<OPTION VALUE="">Pick one:
<OPTION VALUE="1">First
<OPTION VALUE="2">Second
<OPTION VALUE="3">Third
</SELECT>
<TEXTAREA NAME="myText"></TEXTAREA>
<INPUT TYPE="SUBMIT">

Feedback on 'Q360 Is it possible to validate both a pull down menu and a text area when a form is submitted?'

©2018 Martin Webb