JavaScript Validation Method... It seemed so simple.

[FX]Roman

Gawd
Joined
Apr 16, 2002
Messages
819
Hi All,

I've got a form that users can fill out which when submitted will render N number of dropdown fields (via ASP).

For my own reasons, I don't want to store these objects into an array when validating, so the name of each dropdown menu is going to be "#Month" where # will be the number of the dropdown.

So, when submitting the form, I'd like the page to check if everything is filled out, on all forms.


I was thinking something around the lines of this:

Code:
for (var n = 1; n <= <%=request.form("NumberOfEvents")%>; n++){
	if (document.all.[n]Month.options[document.all.[n]Month.selectedIndex] == 0); {
		alert("foo");
	}
}


Can someone point me in the right direction, as this isn't working? :mad: :eek: :( :confused:
 
This is just a guess, but if you are naming the drop down menus with id= or name= I think it can't start with a number, you would for example, have to name it Month1 instead of 1Month. I've never used ASP so I don't know about your syntax though.

 
I don't think the code will work the way you have it.

This portion:
Code:
if (document.all.[n]Month.options[document.all.[n]Month.selectedIndex] == 0); {
		alert("foo");
	}

should be something like:
Code:
//declare var s outside of for loop;

s = n + 'Month';

if (document.all.[s].options[document.all.[s].selectedIndex] == 0); {
		alert("foo");
	}

Though, I'm partial to using getElementById like:

Code:
var s="";
for (n=0;n<= <%request.form("NumberOfEvents")%>;n++){
  s=n + 'Month';
  ele = document.getElementById(s);
  if (ele.selectedIndex==0){
    alert("foo");
  }
}
 
I may be wrong but I believe that
Code:
for (n=0;n<= <%request.form("NumberOfEvents")%>;n++){
needs to be
Code:
for (n=0;n<= <%response.write(request.form("NumberOfEvents"))%>;n++){
 
Hi all, thank you so much for you help so far.

As far as the creation of the For loop, it's working fine as I can "alert" the number of "n" within it as it's looping. Anything else is just not working...

is returning an "Expected identifier" error on page load.

I think I'll just use an array or a dictionary object... Worth a try. :)
 
Gawd, should have gone with an array right off the bat... This is a perfect example of when to use them, however.

All dropdowns are now part of an array and come out named either "Month", "Day" or "Year".

Here's the code:

Code:
for (var n = 0; n <= (<%=request.form("NumberOfEvents")%> - 1); n++){
	
	w = document.all.Month[n].selectedIndex;
	if (w == 0) {
		alert('Please specify a Month value for Event Number '+(n+1));
		document.all.Month[n].focus();
		return(false);
	}
	w = document.all.Day[n].selectedIndex;
	if (w == 0) {
		alert('Please specify a Day value for Event Number '+(n+1));
		document.all.Day[n].focus();
		return(false);
	}
	w = document.all.Year[n].selectedIndex;
	if (w == 0) {
		alert('Please specify a Year value for Event Number '+(n+1));
		document.all.Year[n].focus();
		return(false);
	}	
}
 
Code:
<select name="Month">
	<option value="">Month</option><option value="01">January</option><option value="02">February</option><option value="03">March</option><option value="04">April</option><option value="05">May</option><option value="06">June</option><option value="07">July</option><option value="08">August</option><option value="09">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option>
</select>
<%
Response.Write("<select name=Day>")
	Response.Write("<option value="""">Day</option>")
	for intCounter=1 to 31
		Response.Write("<option value=""" & intCounter & """>" & intCounter & "</option>")
	next
Response.Write("</select>")
%>
<%
Response.Write("<select name=Year>")
	Response.Write("<option value="""">Year</option>")
	for intCounter=0 to 2
		Response.Write("<option value=""" & (Year(date)+intCounter) & """>" & (Year(date)+intCounter) & "</option>")
	next
Response.Write("</select>")
%>
 
I can't answer you directl but this has a page with an input box that must have 000 typed into it in order to allow the other elements to be visibly true.

Code:
<html>
<head>
<script type=text/javascript>
	function activate() {
	        if (document.getElementById('barcode').value == "000") {
	                document.getElementById('newpart').disabled = false;
	                document.getElementById('newcost').disabled = false;
	                document.getElementById('newprice').disabled = false;
	                document.getElementById('npl').disabled = false;
	                document.getElementById('ncl').disabled = false;
	                document.getElementById('nprl').disabled = false;
	        } else {
	                document.getElementById('newpart').disabled = true;
	                document.getElementById('newcost').disabled = true;
	                document.getElementById('newprice').disabled = true;
	                document.getElementById('npl').disabled = true;
	                document.getElementById('ncl').disabled = true;
	                document.getElementById('nprl').disabled = true;
	        }
	}
</script>
</head>

<body>




<table>
<tr>
	<td>Add Part (000 Non-Inventory): </td>
	<td><input type=text length=15 maxlength=13 name=barcode onkeyup="activate()"></td>
</tr><tr>
	<td><label for=newpart disabled id=npl>Desc: </label>		<input type=text size=20 name=newpart disabled id=newpart></td>
	<td><label for=newcost disabled id=ncl>Cost: $</label>		<input type=text size=7 name=newcost disabled id=newcost></td>
	<td><label for=newprice disabled id=nprl>Price: $</label>	<input type=text size=7 name=newprice disabled id=newprice></td>
</tr>
</table>


</body>
</html>
 
Back
Top