469,572 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Need some quick help with a function

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.

--
Adrienne Boswell
http://www.cavalcade-of-coding.info
Please respond to the group so others can share
Aug 19 '05 #1
4 1768
Adrienne wrote:
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>


--
Rob
Aug 19 '05 #2
Gazing into my crystal ball I observed RobG <rg***@iinet.net.au> writing
in news:DH***************@news.optus.net.au:
Adrienne wrote:
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:


<bending down to kiss your feet>Thank you!


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!
--
Adrienne Boswell
http://www.cavalcade-of-coding.info
Please respond to the group so others can share
Aug 19 '05 #3
JRS: In article <DH***************@news.optus.net.au>, dated Fri, 19
Aug 2005 07:16:51, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :

// 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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Aug 19 '05 #4
Dr John Stockton said the following on 8/19/2005 4:44 PM:
JRS: In article <DH***************@news.optus.net.au>, dated Fri, 19
Aug 2005 07:16:51, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
// 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 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".

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Jeff Wagner | last post: by
2 posts views Thread by Mike Button | last post: by
4 posts views Thread by vladimir | last post: by
6 posts views Thread by Chad A. Beckner | last post: by
10 posts views Thread by kj | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.