Need some quick help with a function

A

Adrienne

I am the first to admit that I know bupkis about javascript, except that
sometimes I need it to do something client side that I can't do server
side.

Anyway, here's my problem:

<input type="text" name="ticket" id="ticket">
<input type="text" name="amount" id="amount">

My ASP script defaults ticket to be 30.00 and amount to be 600.00. What
I need is if the visitor puts 50.00 into the ticket field, the amount
field will update to 1000.00 instead of 600.00, in other words, ticket is
5% of amount.

Normally, I would Google and find tutorials or code snippets, but I've
got a deadline. I would really appreciate some help with this.

Thanks in advance.
 
R

RobG

Adrienne said:
I am the first to admit that I know bupkis about javascript, except that
sometimes I need it to do something client side that I can't do server
side.

Anyway, here's my problem:

<input type="text" name="ticket" id="ticket">
<input type="text" name="amount" id="amount">

My ASP script defaults ticket to be 30.00 and amount to be 600.00. What
I need is if the visitor puts 50.00 into the ticket field, the amount
field will update to 1000.00 instead of 600.00, in other words, ticket is
5% of amount.

Normally, I would Google and find tutorials or code snippets, but I've
got a deadline. I would really appreciate some help with this.

A sample is provided with the following comments:

1. If the user does not have javascript enabled or available, the script
will not run. In that case, what is the default behaviour (i.e. what is
the HTML)?

2. In the above case, do you have server-side functionality to perform
the same operations? The 'amount' field should be updated by a trip to
the server.

3. Can the user modify either field, or only the 'ticket' field?

4. Have you designed or implemented any client-side validation?

5. What are valid entries to the 'ticket' field? I've made it a number
with two decimal places. A range of numeric patterns and other tips on
validation are provided here:

<URL:http://www.merlyn.demon.co.uk/js-valid.htm#VNP>


5. You *must* have server-side validation. It would be very easy for a
user to send anything they like back to your server, you can't guarantee
that the script will do what you expect it to even if it runs.

6. toFixed() is widely supported but maybe not widely enough for your
users. You may wish to substitute another method for formatting the
output (hints are on the above site).


<script type="text/javascript">

// Test as decimal number with two decimal places
function validDollars( x ) {
return /^\d+\.\d\d$/.test(x);
}

function upDate( inp, outp ){
var d = inp.value;
if (! validDollars( d ) ) {
alert( 'Input must be a dollar amount like 300.00' );
outp.value = '';
} else {
d *= 5;
outp.value = d.toFixed(2);
}
}

</script>

<form action="">
<p>Please enter ticket amount (e.g. 399.95):
$<input type="text" name="ticket" id="ticket" onchange="
upDate(this,this.form.amount)
"><br>
Total amount:
$<input type="text" name="amount" id="amount" readonly>
</p>
</form>
 
A

Adrienne

A sample is provided with the following comments:


1. If the user does not have javascript enabled or available, the
script will not run. In that case, what is the default behaviour (i.e.
what is the HTML)?

The amount field is prefilled with an amount that is 20 times the ticket
field. This is actually as a convenience for the user. If the user does
not have javascript enabled, it's not a big deal.
2. In the above case, do you have server-side functionality to perform
the same operations? The 'amount' field should be updated by a trip to
the server.

It is.
3. Can the user modify either field, or only the 'ticket' field?

They can modify both.
4. Have you designed or implemented any client-side validation?

No need for client side validation. If there is nothing in the field,
other fields will show 0, which is okay.
5. What are valid entries to the 'ticket' field? I've made it a number
with two decimal places. A range of numeric patterns and other tips on
validation are provided here:

<URL:http://www.merlyn.demon.co.uk/js-valid.htm#VNP>

That's absolutely what I've defaulted to. The server side script alerts if
it is less.
5. You *must* have server-side validation. It would be very easy for a
user to send anything they like back to your server, you can't
guarantee that the script will do what you expect it to even if it
runs.

You're preaching to the choir.
6. toFixed() is widely supported but maybe not widely enough for your
users. You may wish to substitute another method for formatting the
output (hints are on the above site).


<script type="text/javascript">

// Test as decimal number with two decimal places
function validDollars( x ) {
return /^\d+\.\d\d$/.test(x);
}

function upDate( inp, outp ){
var d = inp.value;
if (! validDollars( d ) ) {
alert( 'Input must be a dollar amount like 300.00' );
outp.value = '';
} else {
d *= 5;
outp.value = d.toFixed(2);
}
}

</script>

<form action="">
<p>Please enter ticket amount (e.g. 399.95):
$<input type="text" name="ticket" id="ticket" onchange="
upDate(this,this.form.amount)
"><br>
Total amount:
$<input type="text" name="amount" id="amount" readonly>
</p>
</form>

Works perfectly! Thank you again ever so much!
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 19
Aug 2005 07:16:51, seen in RobG
// Test as decimal number with two decimal places
function validDollars( x ) {
return /^\d+\.\d\d$/.test(x);
}

For the uninitiated, it may be worth observing that the function name is
over-specific; the function works just as well for Pounds, Euros,
Roubles, Rands, etc., and the character before the second slash is not
*semantically* a currency symbol.
 
R

Randy Webb

Dr John Stockton said the following on 8/19/2005 4:44 PM:
JRS: In article <[email protected]>, dated Fri, 19
Aug 2005 07:16:51, seen in RobG



For the uninitiated, it may be worth observing that the function name is
over-specific; the function works just as well for Pounds, Euros,
Roubles, Rands, etc.,

And only the anally retentive notice things like that. To the
"uninitiated" it wouldn't matter, to the initiated, they would know it
didn't matter.
and the character before the second slash is not *semantically* a currency symbol.

Tell Goober I said "Duh huh".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top