Javascript Function with Underscores!?


J

J. Hall

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.value;
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%')
}
}
 
Ad

Advertisements

W

Whitecrest

Hi dudes,
Back at ya...
Got a simple webpage...
There are no simple web pages, only simple web developers...
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
Kind of a blow to the US ego for not being mentioned...
Obviously this mustn't exceed 100% Obviously...

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!

Kind of annoying that it will trigger the function every time you enter
a number don't you think? I enter the first percentage (20%), it alerts
me that it doesn't add up to 100.... I enter the second percentage (30%)
it alerts me again.... I say this is BS and go elsewhere....

Why not have a default value of 0% then when submitting the form check
and reload the page with an error message if they did it wrong? Plus it
would still work when people have Javascript turned off.

Oh, to answer your question, yes, you can have underscores.
 
J

J. Hall

Kind of annoying that it will trigger the function every time you enter
a number don't you think?

Didn't read the whole post did ya!!...

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

Hence autodetecting when the script needs to run, sorted that now and its
running beautifully...except that the whole script fails IF I add
Underscores back into my form name and form input elements!

Anyone else got any ideas?!
Kind of a blow to the US ego for not being mentioned...

Not really, its a European application...

Cheers, @sh
 
Ad

Advertisements

J

J. Hall

Anyone else got any ideas?!
So you don't want the submit to handle it then?

Well, its a nice idea, I see your reasoning, but I'd rather interrupt the
user immediately - when the form loads each percentage box defaults to '0',
its only when all three have been populated to exceed 100 that it fires, if
not it doesn't say a word ;o)
Oh, you originally said world wide... ;-}

Sorry, I live in Somerset (UK), its a small town, you're falling of the edge
if you try to leave hehe...apparently...!

Still puzzled about these underscore! This is the function as it presently
stands, I've tried aliasing the name of the form, and passing it via the
OnChange on the input field
Ash_CalculatePercentageTotal('form_addnewclient') but that doesn't work,
unless I remove the underscore!?!?

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

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

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

if (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');
}
}
}

Appreciate your help dude!¬
 

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

Top