Click to See Complete Forum and Search --> : Javascript looping through dynamic forms
adam9191
04-04-2004, 03:51 PM
Please can someone help out with this? To reiterate, I am looking for a script that will accept an ASP variable for the number of text items on in the form (as this is done dynamically). Then I wish to total the elements (be it 5 or 50) and alert if it is over a specific total, say 100.
It would be something like...Onsubmit the following function would fire:
Loop through all form elements (there will only be text boxes called text<%=n%>)
If form.elements.total > 50
Alert ("you have exceeded your limit, please amend your data")
Hope that helps
Thanks
Khalid Ali
04-04-2004, 04:04 PM
Thats way too simple using DOM API, here is how
in th eonload event call put the following code in the handler
var frm = document.getELementById("formId");
//then you want all the text fields,which are input elements and as I am presuming you have named them all the same, then here is how you can get the total
suppose all the text fields are named as
type="text" name="textElements"
now
var tnum = frm.getElementsByName("textElements");
if(tnum==x){
do this
}else {
do this
}
var t
redijedi
04-04-2004, 04:26 PM
Since you seem to be using asp, you may want to backup Khalid Ali's with a server side solution.
It’s been a while since I've worked with ASP, but I believe all of your form elements should be in the Request object. Just use a for loop starting at 0 and continuing through the length of the Request.Form object. In each iteration of the loop you can total up the entries (and perform other validation). Upon exiting the loop you can output whatever message that you like to the user.
PSEUDOCODE (my vbscript is rusty):
Dim total as integer = 0
For i=0 to Request.Form(element).Count
total += Request.Form(element).value
next
If total > 50 Then
Response.Write(“you have exceeded your limit, please amend your data")
End If
adam9191
04-04-2004, 05:05 PM
I am getting the following:
The function expects a string as input.
What is going wrong, here is the code im using..
<%
Dim total
total = 0
For i=0 to Request.Form(element).Count
total= total + Request.Form(element).value
next
If total > 50 Then
Response.Write("you have exceeded your limit, please amend your data")
End If
%>
Can anyone help?
redijedi
04-04-2004, 06:46 PM
As I said, my VBScript is rusty. The code would look something more like this.
' Check to see if any form varibles exist
if len(request.form) > 0 then
for each variable_name in request.form
total += Request.Form(variable_name).value
next
end if
The problem is that for some reason, ASP takes all the inputs in the form and concatenates them with commas into a comma delineated list and sets this list as the value of the first element in the form. This seems crazy to me, and I don’t remember dealing with this when I used to be more heavily involved in ASP.
I’ll troll the ASP forums for a solution. In the mean time, it may be worth it to start a thread in the ASP forum referencing this problem.
redijedi
04-04-2004, 07:16 PM
Okay, nevermind. My ASP install was just acting up it appears. Here's some sample.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim total
total = 0
' Check to see if any form varibles exist
If Len(Request.Form) > 0 Then
' Loop through each form variable
For Each variable In Request.Form
' We only want the text fields, not the submit button
If Not variable = "submit" Then
variable_value = Request.Form(variable)
' Make sure that the variable is a number
If IsNumeric(variable_value) Then
total = total + variable_value
End If
End If
Next
' If the total is greater than some value, display a message
If total > 50 Then
Response.Write("you have exceeded your limit, please amend your data")
End If
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="this.asp" method="post" >
<input name="text1" type="text"><br>
<input name="text2" type="text"><br>
<input name="text3" type="text"><br>
<input name="text4" type="text"><br>
<input name="text5" type="text"><br>
<input name="text6" type="text"><br>
<input name="submit" type="submit">
</form>
</body>
</html>
adam9191
04-05-2004, 03:21 AM
Ive sorted it with this code...
<%
Dim total
total = 0
For i = 1 To Request.Form("element").Count
total = total + Request.Form("element")(i)
Response.Write Request.Form("element")(i) & "<BR>"
Next
If total > 50 Then
Response.Write("you have exceeded your limit, please amend your data")
End If
%>
BUT....how can I get it to loop through elements that are dynamically named. For example, text01, text, 02, text112. The numbers represent order ids. Am I able to substitute the order id into the code above, for example, <% = id %>
redijedi
04-05-2004, 05:00 AM
This section of code allows you to work with the data without knowing their names:
' Loop through each form variable
For Each variable In Request.Form
' We only want the text fields, not the submit button
If Not variable = "submit" Then
variable_value = equest.Form(variable)
' Make sure that the variable is a number
If IsNumeric(variable_value) Then
total = total + variable_value
End If
End If
Next
' If the total is greater than some value, display a message
If total > 50 Then
Response.Write("you have exceeded your limit, please amend your data")
End If