how do i restricted the input in other boxes imediately...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kol090
Forum Newbie
Posts: 5
Joined: Mon Sep 19, 2005 8:46 am

how do i restricted the input in other boxes imediately...

Post by kol090 »

after user inputs number.

hi

i want to make three input boxes, for a game, i want the player to input
three separate values which must all add up to a 100. i don't know what to use
im currently using php, but i don't know if i should incorperate some javascript
to this situation. using either select or text boxes.


this is the code of the html

<td> <input type="text" name="agg5" size ="2"> </td>
<td> <input type="text" name="pat5" size ="2"> </td>
<td> <input type="text" name="tri5" size ="2"> </td>


i want the person to enter the first number, then i make sure it is below a hundered and above 0
while also restricting their second input to below 100 - first_input and above 0
then the third to below 100-first_input+second input

thank you
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

definatly going to be using javascript unless you want the user to click submit like a hundred times.

what i would do is have a counter at the top of the forum starting with 100. have the default value be 0 for each field then use onBlur to calculate the difference of 100 and the 3 boxes, then put that number in the counter. if they go below 0 then give a ALERT error. should not be too difficult. then you can use php to do the last bit of error checking incase they where trying to sneak a quick one past ya.

understand?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you want to actively do that restriction you must do it with Javascript. However, you'll still have to verify the result on the form processing side...

The Javascript would likely use the onchange event to run some basic code that summed the values of each of the fields to determine if the current on is out of range or not...
kol090
Forum Newbie
Posts: 5
Joined: Mon Sep 19, 2005 8:46 am

Post by kol090 »

thanks for trying to understand my bad writing

but do u know the code for the javascript?
onchange seems right but how do i use it, and will it allow me to change/check on the same page?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in your case, you'd use the Javascript function parseInt() on each of the "other" values, so if you were in field two, you'd parseInt() the first and third fields' value properties. Afterwhich, you add them together and subtract that from 100.. the result is the allowed range of that field. Simply parseInt() that field's value property and compare...
Post Reply