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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Checking Dynamic HTML from ASP 1

Status
Not open for further replies.

scripter50

IS-IT--Management
Dec 9, 2002
35
0
0
US
I have an ASP page generating html code like this...

<%for i = 1 to nNbrOfDays%>

<table>
<tr>
<td>Day <%=i%>&nbsp;<input type="text" name="txtDate<%=i%>">td>

<td><select name="cboService<%=i%>" style="width:120" onchange="fnPopVisits<%=i%>();">
<option value="0">No Service</option>
<option value="1">Daily Visits</option>
<option value="2">Live-In</option>
</select>
</td>
etc...etc...
<% next %>
<input type="submit" name="btnSubmit" value="Proceed" onclick = "return fnValidate();"> ....

I'm trying to do an edit check with javascript to prevent empty fields..

<javascript>
funtion fnValidate() {
var nbrofdays = document.frmNewReservation.hdnNbrOfDays.value;
for (i = 1; i<=nbrofdays; i++) {
if (document.frmNewReservation.txtDate.value =="") {
alert("Enter value..");
return false;
}
}
</script>

I am not able to access the form element this way, "txtDate" is not available??? Any ideas?

Thanks in advance,
scripter50
 
You dont have an array named txtDate

What you have is a bunch of items named:
txtDate1
txtDate2
txtDate3
...
txtDateX

Where X = nNbrOfDays



PS: It looks like there is a missing "</"
<input type="text" name="txtDate<%=i%>">[red]</[/red]td>


 
Oh, and your script starts with <javascript> but ends with </script>

....

Maybe try something like this:
Code:
<script>
funtion fnValidate() 
{
 var nbrofdays = document.frmNewReservation.hdnNbrOfDays.value;
 for (i = 1; i<=nbrofdays; i++) 
  {
   if (document.all("txtDate" + i.toString) == "")
    { 
     alert("Enter value..");
     return false;
    }
  }
 return true;
}
</script>

PS: This is really more of a JavaScript question than an ASP question to the extent that it deals with client-side rather than server-side validation... so you might try asking in the JavaScript forum: forum216
 
Thanks Sheko for your quick Sunday response! Also, sorry for my abbreviated code in my post, just trying to save space. Also, I thought of posting in javascript, but not sure if user would understand the asp generated html!

I'll give your suggestion a go. Is there a way to put all of the form variables into an array so I could access them as I thought?

Thanks again,
scripter50
 
here is how you can loop through all form fields...you can also put them into an array if you want...

Code:
For each inputField in Request.Form
	For each inputValue in Request.Form(inputField)
		response.write inputField  & " = " & inputValue & "<
		br>"
	Next
Next

-DNG
 
Sheko,
Doesn't work? The script doesn't see any empty fields? Do I need to put the values into an array before the document.all call? I'm confused! My js manual shows document.all, but no reference to document.all("formfield")??
Thanks again,
Scripter50
 
Sheko,
Never mind, I needed to put .value in the string, it works fine.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top