Click to See Complete Forum and Search --> : Javascript Maths using form values


SpaceMonkey
11-22-2003, 10:23 AM
Hi All,

I am building an ASP web application which is a hotel reservation form. I am using Javascript to validate and submit the form. The forms structure changes depending on the users inputs, therefore, the name and number of fields make it very difficult to code. The section of the form which deals with the rooms looks like:

For each room:
<input type="hidden" name="rm1id" value="1">
<input type="hidden" name="rm1beds" value="1">
<input type="text" name="rm1" size="4" maxlength="2" value="0"> Single Rooms required<br>

The number for rm1id, rm1beds and rm1 is generated by a counter.
The value for rm1id is the database number for the room.
The value for rm1beds is the number of people the room can hold.
The name form rm1 is generated via the database.

After all the rooms have been displayed there is one more field which looks like:
<input type="hidden" name="numofrooms" value="3">
Where the value is generated by the counter.

On submit I need to check that the rooms hold enough people.

Therefore, I need a total which goes through each room and multiply's the rm?beds by rm? and adds it to the total.

Once I have a grandtotal, I need to check that it is greater that an ASP Session Variable which is Session("PartySize").




I have been trying for 4 days and just cannot get my head around this. Can anyone help?

Much Appreciated
SpaceMonkey

ray326
11-22-2003, 11:08 AM
Why do this in Javascript? Go with the (ASP) flow and do this on the server in VBscript using the content of the form object in the request.

SpaceMonkey
11-22-2003, 11:17 AM
The client has specifically requested that the errors are generated using JavaScript so that he can alter them after I finish the project.

Also, the customers for his sites are not necessarily, experienced internet users. Therefore, front-end validation will help them.

Finally, repopulating the form after the error is nearly impossible due to the dynamic structure. If I do this in ASP the development time will nearly double.

Thanks for you suggestion though.
SpaceMonkey.

Ninjagranny
11-22-2003, 11:47 AM
can you use the form.formname.value

or a this.value.valuedescription to poke them into a filed that javascript can use and then add them up ?

Just thoughts ,, maybe it will help

SpaceMonkey
11-23-2003, 02:48 AM
Ninjagranny,

Thanks. However, I was really looking for some more comprehensive code.

I am having another go and should I get something going, I will post it to give you guys a starting poing.

SpaceMonkey

SpaceMonkey
11-23-2003, 04:02 AM
Here's the code I'm trialing so far.

Dim RMHoldsCounter As Integer = 1
While Not RMHoldsCounter = CInt(Request.Form("numofrooms"))
Response.Write("var rm" & RMHoldsCounter & "holds = parseInt(form.rm" & RMHoldsCounter & ".value) * parseInt (form.rm" & RMHoldsCounter & "beds.value);")
RMHoldsCounter = RMHoldsCounter + 1
Wend
RMHoldsCounter = 1
While Not RMHoldsCounter = CInt(Request.Form("numofrooms"))
Response.Write("var totalholds = 0;")
Response.Write("var totalholds = totalholds + rm" & RMHoldsCounter & "holds;")
RMHoldsCounter = RMHoldsCounter + 1
Wend
Set RMHoldsCounter = Nothing

The Dim is causing an "End of statement expected" error at the minute. I don't know why. However, if I don't declare it as an Integer it's goes into an infinite loop.

ray326
11-23-2003, 12:38 PM
That's not Javascript.

Ninjagranny
11-23-2003, 06:18 PM
Just what I was thinking but - I didnt want to say - Just in case i was wrong :)