Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to validate data from a dropdown or select field

Status
Not open for further replies.

vward

MIS
Oct 3, 2000
1
CA
I have a form which has drop down boxes or select fields in it. I need that data submitted to be validated as with the other text fields but the usual validation statement does not seem to work. Here is what I have for the javascript: -

if (document.frmClientInfo.REQMONTH.value == "") {
alert("You must fill in 'Month Required' before submitting.");
document.frmClientInfo.REQMONTH.focus();
return false;
}

Here is what I have in the HTML: -

<select name=&quot;REQMONTH&quot; size=&quot;1&quot;>
<option value=&quot;&quot;></option>
<option value=&quot;January&quot;>January</option>

The validation always works for a text field but does not work for the dropdown if not selected by my users.

What am I doing wrong? [sig][/sig]
 
Not sure why your's isn't working but here is a working example (tested on IE5.5 and NN6)...

Code:
<html>
<head>
<script language=&quot;javascript&quot;>
	function fnValidateMonth(){
		var z=document.frmForm.selMonth;
		if (z.value == 0) {
	    alert(&quot;You must fill in 'Month Required' before submitting.&quot;);
	    z.focus();
	    return false;
	  }
	}
</script>
</head>
<body>
<form id=frmForm name=frmForm>
Month Required:
<select id=selMonth name=selMonth onChange=&quot;fnValidateMonth()&quot;>
	<option value=0 selected>-Select a month-</option>
	<option value=1>January</option>
	<option value=2>March</option>
	<option value=3>April</option>
	<option value=4>May</option>
</select>
</form>

You can change the values of the options to anything. I used numbers because it makes date calculation that much easier.

Hope it helps, [sig]<p>Rob<br><a href=mailto:robschultz@yahoo.com>robschultz@yahoo.com</a><br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
"Focus on the solution to the problem,<br>
not the obstacles in the way."<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top