Validating a SELECT form element

Results 1 to 4 of 4

Thread: Validating a SELECT form element

  1. #1
    Zach Richards Guest

    Default Validating a SELECT form element

    I need a validation script for a SELECT form element. Just to make sure the user has selected something other than the default or selected OPTION. Any help would be great!<BR>Thanks,<BR><BR>Zach

  2. #2
    Join Date
    Dec 1969

    Default RE: Validating a SELECT form element

    Zach,<BR><BR>What you can do is write some validation code in your action page which checks to make sure the value of the SELECT form element isn&#039t the same as the OPTION tag for the default.<BR><BR>For example:<BR><BR>Here&#039s your form.<BR><BR>&#060;FORM ACTION="check_form.asp" METHOD="POST"&#062;<BR>&#060;SELECT NAME="selDogs"&#062;<BR>&#060;OPTION VALUE=0 SELECTED&#062;Please pick a dog<BR>&#060;OPTION VALUE=1&#062;Bulldog<BR>&#060;OPTION VALUE=2&#062;Poodle<BR>&#060;OPTION VALUE=3&#062;Dalmation<BR>&#060;/SELECT&#062;<BR><BR>And here&#039s an excerpt from check_form.asp<BR><BR>&#060;%<BR>valDogs = Request.Form("selDogs")<BR>If valDogs = 0 Then<BR> errMsg = "You must choose a dog from the list"<BR>End if<BR><BR>** You can put other validation checking here **<BR><BR>If errMsg &#060;&#062; "" Then<BR> Response.write "The following errors occurred: " & errMsg<BR> Response.write "Click the back button on your browser to go back and correct the errors"<BR> Response.end<BR>Else<BR> ** Continue on with the rest of the page**<BR>End if<BR><BR>Of course you could populate the select list with info from the database but I put static values in there for timesake.<BR><BR>Hope that helps.<BR><BR>Keith

  3. #3
    Sam Otto Guest

    Default RE: Validating a SELECT form element

    I frequently have added to my &#060;FORM&#062; tag a OnSubmit="return validate();" and written a validate() function in JavaScript that checks various fields in the form. If something is wrong, I put up an alert set focus to the field in question and then return false. By returning false, the form isn&#039t submitted. So all you need to do is check that the selectedIndex is not 0.

  4. #4
    Vikram Koneri Guest

    Default RE: Validating a SELECT form element

    Sam Otto&#039s suggestion is most useful.<BR>May be u need the correct syntax to access the selected index of SELECT Tag. If so here it is.<BR><BR>To Access the Index use below :<BR> document.MySelect.selectedIndex<BR>Note that &#039I&#039 in &#039selectedIndex&#039 is capital<BR><BR>To access the Value or Text of the selected index use the below syntax:<BR>document.MySelect.options[document.MySelect.selectedIndex].value<BR>document.MySelect.options[document.MySelect.selectedIndex].text<BR><BR>Hope its UseFul<BR>Cheers<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts