By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

Simple Function - but doesn't work!

P: n/a
Hi dudes,

Got a simple webpage, with three numeric text input boxes, the idea being
that the user is asked to insert percentages of their business around the
world...

UK, Europe, Other

Obviously this mustn't exceed 100% and so I OnChange I simply want to check
that all three boxes have a value, and if so sum them up and alert the user
is the sum exceeds 100!

I have constructed the function below that WORKS but ONLY IF I remove the
underscores from the Form Name and Input Boxes!? I'm totally confused by
this, I haven't had this occurance before, and so I'm wondering if
underscores are allowed in functions?

Finally, how do I add an IF statement before the main IF statement to check
for values in all three boxes?

Many thanks!

Please help!

-------------------------------

function Ash_CalculatePercentageTotal() {
Num1 = document.form_addnewclient.Q_TurnoverPercentUK.val ue;
Num2 = document.form_addnewclient.Q_TurnoverPercentEurope .value;
Num3 = document.form_addnewclient.Q_TurnoverPercentOther. value;

alert('For debugging: ' + Num1 + ' ' + Num2 + ' ' + Num3)

if (eval(Number(Num1)+Number(Num2)+Number(Num3)) > 100) {
alert('Please check the percentages entered, they must add up to 100%')
}
}

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lee
J. Hall said:

Hi dudes,

Got a simple webpage, with three numeric text input boxes, the idea being
that the user is asked to insert percentages of their business around the
world...

UK, Europe, Other

Obviously this mustn't exceed 100% and so I OnChange I simply want to check
that all three boxes have a value, and if so sum them up and alert the user
is the sum exceeds 100!

I have constructed the function below that WORKS but ONLY IF I remove the
underscores from the Form Name and Input Boxes!? I'm totally confused by
this, I haven't had this occurance before, and so I'm wondering if
underscores are allowed in functions? function Ash_CalculatePercentageTotal() {
Num1 = document.form_addnewclient.Q_TurnoverPercentUK.val ue;
Num2 = document.form_addnewclient.Q_TurnoverPercentEurope .value;
Num3 = document.form_addnewclient.Q_TurnoverPercentOther. value;

alert('For debugging: ' + Num1 + ' ' + Num2 + ' ' + Num3)

if (eval(Number(Num1)+Number(Num2)+Number(Num3)) > 100) {
alert('Please check the percentages entered, they must add up to 100%')
}
}


If it works without the underscores, then my first guess
is that the actual names in your HTML form don't have
underscores, but you've neglected to show that code.

What happens when it doesn't work? What error message
do you see, or what bad result do you get?

You don't need to use eval() to sum numbers.

What happens if the sum is less than 100?

Jul 23 '05 #2

P: n/a
The underscores are in the code, the error message received is that 'the
object could not be found' for each of the three elements, or that it is
null - I can assure you the code is accurate from that point of view.

Didn't realise I didn't need Eval, thanks for that. Still very odd relating
to underscores, but I'll do some more investigation...

Cheers, @sh
"Lee" <RE**************@cox.net> wrote in message
news:c9*********@drn.newsguy.com...
J. Hall said:

Hi dudes,

Got a simple webpage, with three numeric text input boxes, the idea being
that the user is asked to insert percentages of their business around the
world...

UK, Europe, Other

Obviously this mustn't exceed 100% and so I OnChange I simply want to checkthat all three boxes have a value, and if so sum them up and alert the useris the sum exceeds 100!

I have constructed the function below that WORKS but ONLY IF I remove the
underscores from the Form Name and Input Boxes!? I'm totally confused by
this, I haven't had this occurance before, and so I'm wondering if
underscores are allowed in functions?

function Ash_CalculatePercentageTotal() {
Num1 = document.form_addnewclient.Q_TurnoverPercentUK.val ue;
Num2 = document.form_addnewclient.Q_TurnoverPercentEurope .value;
Num3 = document.form_addnewclient.Q_TurnoverPercentOther. value;

alert('For debugging: ' + Num1 + ' ' + Num2 + ' ' + Num3)

if (eval(Number(Num1)+Number(Num2)+Number(Num3)) > 100) {
alert('Please check the percentages entered, they must add up to 100%')
}
}


If it works without the underscores, then my first guess
is that the actual names in your HTML form don't have
underscores, but you've neglected to show that code.

What happens when it doesn't work? What error message
do you see, or what bad result do you get?

You don't need to use eval() to sum numbers.

What happens if the sum is less than 100?

Jul 23 '05 #3

P: n/a
J. Hall wrote:
The underscores are in the code, the error message received
is that 'the object could not be found' for each of the three
elements, or that it is null - I can assure you the code is
accurate from that point of view.
We can take your word for it that the accompanying HTML corresponds with
your property accessors, in which case you are on your own, or you can
show us the HTML so we can see if there are any obvious reasons for the
failure of your script.

As it stands we donít know which browser is reporting these errors, 'the
object could not be found' doesnít sound like an IE error report. We don
ít know whether the HTML is HTML or XHTML (or XHTML being interpreted as
HTML (appendix c compatibility XHTML)). We donít know if the (X?)HTML is
valid. We donít know whether the identifiers in the script are
referencing named form elements of IDed elements (or both). And then
there are numerous other possible factors that might be impacting on
your situation that would just take a moments visual inspection of the
HTML to identify or rule out.

Ultimately your best cause of action is to create a test case page,
stripped down to just the form and accompanying (relevant) script, that
demonstrates the problem. Posting that and allowing us to attempt to
re-produce it in the browser that you are using, and others, will be
your quickest route to understanding the problem. At least partly
because making a striped down test case page often reveals the cause of
the problem along the way (when you discover that stripping something
that seemed irrelevant out coincidentally makes the problem vanish).
Didn't realise I didn't need Eval, ...

<snip>

There are so few genuine reasons for using - eval - that it is better to
assume you don't ever need it. By the time you know enough to encounter
a situation that might call for its use you should be able to be certain
of that for yourself.

Richard.
Jul 23 '05 #4

P: n/a
Try this version, using parseFloat is easier, and the function, allowing now
for arguments, is more flexible. I hope this helps

<script language="JavaScript"><!--

function Ash_CalculatePercentageTotal(arrayOfNames){
/*
@arrayOfNames= array of input fields without appending value: instance: new
Array(document.formName.field1, document.formName.field2)
*/
if(typeof(arrayOfNames)!="object"){return false;};
var num=0;
for(var i=0; i<arrayOfNames.length; i++){
num+=parseFloat(arrayOfNames[i].value);
}
if(isNaN(num)|| num>100){
alert("Please check the percentages entered, they must add up to 100% and
must be numerical values"); return false;
}
/*you can delete this->*/alert(num);
return num;
}

//--></script>
</head>

<body bgcolor="#ffffff" text="#000000">
<form name="foo">
<input name="name1">
<input name="name2">
<input name="name3">
<input type="button" onClick="Ash_CalculatePercentageTotal( [
document.foo.name1, document.foo.name2, document.foo.name3 ] )"
value="Test">
</form>

ciao
Alberto
http://www.unitedscripters.com/

"J. Hall" <re*************@a-hall.com> ha scritto nel messaggio
news:10**************@news01.eclipse.net.uk...
Hi dudes,

Got a simple webpage, with three numeric text input boxes, the idea being
that the user is asked to insert percentages of their business around the
world...

UK, Europe, Other

Obviously this mustn't exceed 100% and so I OnChange I simply want to check that all three boxes have a value, and if so sum them up and alert the user is the sum exceeds 100!

I have constructed the function below that WORKS but ONLY IF I remove the
underscores from the Form Name and Input Boxes!? I'm totally confused by
this, I haven't had this occurance before, and so I'm wondering if
underscores are allowed in functions?

Finally, how do I add an IF statement before the main IF statement to check for values in all three boxes?

Many thanks!

Please help!

-------------------------------

function Ash_CalculatePercentageTotal() {
Num1 = document.form_addnewclient.Q_TurnoverPercentUK.val ue;
Num2 = document.form_addnewclient.Q_TurnoverPercentEurope .value;
Num3 = document.form_addnewclient.Q_TurnoverPercentOther. value;

alert('For debugging: ' + Num1 + ' ' + Num2 + ' ' + Num3)

if (eval(Number(Num1)+Number(Num2)+Number(Num3)) > 100) {
alert('Please check the percentages entered, they must add up to 100%')
}
}


Jul 23 '05 #5

P: n/a
Hopefully this'll clear up a few elements...

Straight ASP page with VB script, writing in standard HTML for form elements
etc - here's the actual form itself...

This is the version that works, as soon as I ad underscores into the form
and input field names (within the body, and the function itself), it fails!!

----------------------------------------------------------------------------
--------------

<form action="process_clients.asp" method="post" name="formaddnewclient">
<table width="600" height="25" border="0"
cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle"><img
src="graphics/common/horizontalline_plain.gif" width="600" height="1"></td>
</tr>
</table>
<table width="600" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td align="center" valign="middle"
class="MainTextBody">&nbsp;</td>
<td>&nbsp;</td>
<td class="MainTextBody">&nbsp;</td>
</tr>
<tr>
<td width="30" align="center" valign="middle"
class="MainTextBody"><img src="graphics/bullets/medium.gif" width="6"
height="6"></td>
<td width="10"><img src="spacer.gif" width="10"
height="3"></td>
<td class="MainTextBody">Percentage of estimated
turnover</td>
</tr>
</table>
<table width="600" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td width="30" align="center" valign="middle"
class="MainTextBody">&nbsp;</td>
<td width="10"><img src="spacer.gif" width="10"
height="3"></td>
<td width="125" class="MainTextBody">UK
<input name="QTurnoverPercentUK" type="text"
class="AddAClientTextInput" id="QTurnoverPercentUK"
onChange="javscript:Ash_CalculatePercentageTotal(' formaddnewclient');"
value="0" size="6" maxlength="3">
%</td>
<td width="125" class="MainTextBody">Europe
<input name="QTurnoverPercentEurope" type="text"
class="AddAClientTextInput" id="QTurnoverPercentEurope"
onChange="javscript:Ash_CalculatePercentageTotal(' formaddnewclient');"
value="0" size="6" maxlength="3">
%</td>
<td width="125" class="MainTextBody">Other
<input name="QTurnoverPercentOther" type="text"
class="AddAClientTextInput" id="QTurnoverPercentOther"
onChange="javscript:Ash_CalculatePercentageTotal(' formaddnewclient');"
value="0" size="6" maxlength="3">
%</td>
<td class="MainTextBody"><img src="spacer.gif"
width="178" height="8"></td>
</tr>
</table>
<table width="600" height="25" border="0"
cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle"><img
src="graphics/common/horizontalline_plain.gif" width="600" height="1"></td>
</tr>
</table>
</form>

----------------------------------------------------------------------------
--------------

....now the function itself

----------------------------------------------------------------------------
--------------

function Ash_SubmitForm(TheFormName) {
eval('document.'+TheFormName+'.submit();')
}

function Ash_CalculatePercentageTotal(TheForm) {
var Num1 = eval('document.'+TheForm+'.QTurnoverPercentUK.valu e');
var Num2 = eval('document.'+TheForm+'.QTurnoverPercentEurope. value');
var Num3 = eval('document.'+TheForm+'.QTurnoverPercentOther.v alue');

if (Num1 > "" && Num2 > "" && Num3 > "") {

if (eval(Number(Num1)+Number(Num2)+Number(Num3)) > 100) {
alert('Please check the percentages entered, they must add up to
100%\n\nIf a region is equal to 0%, please enter a 0');
}
}
}


"Richard Cornford" <ri*****@litotes.demon.co.uk> wrote in message
news:c9**********@sparta.btinternet.com...
J. Hall wrote:
The underscores are in the code, the error message received
is that 'the object could not be found' for each of the three
elements, or that it is null - I can assure you the code is
accurate from that point of view.


We can take your word for it that the accompanying HTML corresponds with
your property accessors, in which case you are on your own, or you can
show us the HTML so we can see if there are any obvious reasons for the
failure of your script.

As it stands we don't know which browser is reporting these errors, 'the
object could not be found' doesn't sound like an IE error report. We don
't know whether the HTML is HTML or XHTML (or XHTML being interpreted as
HTML (appendix c compatibility XHTML)). We don't know if the (X?)HTML is
valid. We don't know whether the identifiers in the script are
referencing named form elements of IDed elements (or both). And then
there are numerous other possible factors that might be impacting on
your situation that would just take a moments visual inspection of the
HTML to identify or rule out.

Ultimately your best cause of action is to create a test case page,
stripped down to just the form and accompanying (relevant) script, that
demonstrates the problem. Posting that and allowing us to attempt to
re-produce it in the browser that you are using, and others, will be
your quickest route to understanding the problem. At least partly
because making a striped down test case page often reveals the cause of
the problem along the way (when you discover that stripping something
that seemed irrelevant out coincidentally makes the problem vanish).
Didn't realise I didn't need Eval, ...

<snip>

There are so few genuine reasons for using - eval - that it is better to
assume you don't ever need it. By the time you know enough to encounter
a situation that might call for its use you should be able to be certain
of that for yourself.

Richard.

Jul 23 '05 #6

P: n/a
J. Hall wrote:
Hopefully this'll clear up a few elements...

Straight ASP page with VB script, writing in standard HTML for form
elements etc - here's the actual form itself...

This is the version that works, as soon as I ad underscores into the
form and input field names (within the body, and the function
itself), it fails!!
So I propose you create a test case page that demonstrates the problem
in isolation and you respond by posting code that doesn't exhibit the
problem, but does include a mass of irrelevant noise.
<form action="process_clients.asp" method="post"
name="formaddnewclient">
<table width="600" height="25" border="0"
cellpadding="0" cellspacing="0"> .... </table>
<table width="600" border="0" cellspacing="0"
cellpadding="0"> .... </table>
<table width="600" border="0" cellspacing="0"
cellpadding="0"> .... </table>
<table width="600" height="25" border="0"
cellpadding="0" cellspacing="0"> .... </table>
</form>
You badly need to learn HTML and CSS. This code is really bloated for
what it is. Just removing the obvious redundancies will half its size,
knowing how to author and style HTML would reduce the total by about a
factor of 20.

Bloated HTML has obvious consequences for the user's download time,
server load and bandwidth consumption. Those may not be considered
significant (at least by those that think everyone (at least everyone
that matters) is on broadband), but as soon as a web site goes beyond
the machine (Dreamweaver) generation of HTML and starts employing people
to manipulate that HTML (sever or client-side) unnecessary code bloat
starts to impact on development costs. Because the more HTML there is
the longer it takes to find anything in that HTML. Maybe often just
fractions of a second longer but it adds up, and it adds up at expensive
software developer hourly rates.

There is also an increasing tendency to attempt to use HTTP request
objects to avoid re-loading web pages, with their consequential
javascript dependencies, limited browser support and increased
complexity. I often wonder to what extent this trend is motivated by
HTML that has become so bloated that an ordinary HTTP request followed
by loading a page from the server has become so slow as to motivate its
authors to look for an alternative. Solving the wrong problem, when
authoring efficient HTML would have avoided the problem manifesting
itself in the first place.
function Ash_SubmitForm(TheFormName) {
I found no evidence that of this function being called at any point. As
it is the only way that the form can be submitted I am forced to
question you assertion that this code "works".
eval('document.'+TheFormName+'.submit();')
When I said that it was best to assume that you never need to use -
eval - I meant it. Using - eval - to execute dynamically constructed dot
notation property accessors is always unnecessary as javascript offers
bracket notation property accessors as an alternative, and they
facilitate the dynamic construction of property names:-

document[TheFormName].submit();
}

function Ash_CalculatePercentageTotal(TheForm) {
var Num1 = eval('document.'+TheForm+'.QTurnoverPercentUK.valu e');
var Num1 = document[+TheForm].QTurnoverPercentUK.value;
var Num2 = eval('document.'+TheForm+'.QTurnoverPercentEurope. value');
var Num2 = document[TheForm].QTurnoverPercentEurope.value;
var Num3 = eval('document.'+TheForm+'.QTurnoverPercentOther.v alue');
var Num3 =document[TheForm].QTurnoverPercentOther.value;
if (Num1 > "" && Num2 > "" && Num3 > "") {

if (eval(Number(Num1)+Number(Num2)+Number(Num3)) > 100) {
if ((Number(Num1)+ Number(Num2)+ Number(Num3)) > 100) {

- or:-

if (((+Num1)+(+Num2)+(+Num3)) > 100) {

(Additional checks are required in this testing as a user entering
non-numeric data will result in that field being converted to NaN and so
the sum will be NaN and NaN is never > 100 (or < 100))
alert('Please check the percentages entered, they must add up to
100%\n\nIf a region is equal to 0%, please enter a 0');
}
}
}
<URL: http://jibbering.com/faq/#FAQ4_39 >

One of the factors (though a relatively minor one) that motivates people
to recommend against using - eval - is that it re-locates and conceals
error producing code.

The underscore character is allowed in any location within a javascript
identifier and dot notation property accessors only require that valid
identifiers are used (bracket notation is not so restrictive). There is
nothing here that would suggest a reason why the code that you are not
showing us is not working.

On the other hand, I wonder about the logic of the process and the form.
You are asking for percentages of turnover originating in the UK, Europe
and Other, and insisting that those percentages add up to 100%. However,
those three groups cover everywhere, so why can't the user fill in
Europe and UK figures and leave your (server-side) code to work out that
Other must be whatever is left when UK and Europe is subtracted from
100%. The form would be easier to fill in and you would get exactly the
same information from it.
"Richard Cornford" <ri*****@litotes.demon.co.uk> wrote ...

<sniped verbatim quote of preceding message>

Please do not top-post to comp.lang.javascript. If you don't want people
to help you it would be simple to not post at all.

Richard.
Jul 23 '05 #7

P: n/a
JRS: In article <c9*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted at Thu, 3 Jun 2004 16:26:34 :

if (((+Num1)+(+Num2)+(+Num3)) > 100) {

(Additional checks are required in this testing as a user entering
non-numeric data will result in that field being converted to NaN and so
the sum will be NaN and NaN is never > 100 (or < 100))


No, not additional. Either of the following should suffice :

if ( ! (((+Num1)+(+Num2)+(+Num3)) <= 100) ) {
if ( ! ( +Num1 + +Num2 + +Num3 <= 100) ) {

Consider them as if not right then ...

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.