473,414 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 1947
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
4
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
14
by: J. Makela | last post by:
Hallo. This should be a pretty entertaining question for you *real* javascript writers.. I, being the lowly photoshop guy at an ad agency made the mistake of actually saying "HTML" in a...
2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
7
by: lkrubner | last post by:
The PHP scripting language has the array_unique() function that gets the unique, non-redundant values out of an array. Does Javascript have anything similar?
4
by: vladimir | last post by:
All, I have seemingly quite known problem, namely I need to watch overloaded in quick watch(by own vector) but I can not. It says overloaded operator is not found. And that is claimed normal...
6
by: Chad A. Beckner | last post by:
Does anyone know how to make common database connections "common" in all aspx files? In other words, instead of creating a SQLConnection, then a DataAdapter in every ASPX file for my application,...
3
by: AAOMTim | last post by:
In my design, page 1 is a web-based dashboard used to assign resources to an organization. The dashboard includes information about the organization as well as a list of all assigned resources. ...
2
by: yela | last post by:
I am working in a floting menu for a website, I found this cool script, it works great, but I need to limit the movement of the menu to a table area, right now it goes up and down through the whole...
10
by: kj | last post by:
Hi. I'd like to port a Perl function that does something I don't know how to do in Python. (In fact, it may even be something that is distinctly un-Pythonic!) The original Perl function takes...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.