/    Sign up×
Community /Pin to ProfileBookmark

plz hlp with simple script

i have this function:

[CODE]
function suma(uno, dos, tres, cuatro, cinco, seis){
if(uno.length < 1)
uno = 0;
if(dos.length < 1)
dos = 0;
if(tres.length < 1)
tres = 0;
if(cuatro.length < 1)
cuatro = 0;
if(cinco.length < 1)
cinco = 0;
if(seis.length < 1)
seis = 0;

res = 0;
res = uno + dos + tres + cuatro + cinco + seis;

if(res == 100)
return true;
else
return false;

}
[/CODE]

I call taht funcion like this:

[CODE]
function validateForm(f){

(!suma(f.compraingram.value, f.comprasynnex.value, f.compramexmal.value, f.compracentel.value, f.compramaps.value, f.compraotro.value))
? showImage(“compra”, “estrella.gif”, true)
: showImage(“compra”, “empty.gif”, false);


}
[/CODE]

my form look like this:

[CODE]<form name=”form1″ method=”post” action=”preregistro3.php” onSubmit=”return validateForm(this)”>[/CODE]

The problem is that my function suma always return false…
I notice that my variable ‘var’ never match if i use add but it match if i use / or *

any idea on what i am doing wrong?

Thx

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoNov 01.2005 — Well, it will only return true if your value is equal to 100. As long as you send it six integers that add up to 100 in anyway then it should return true. (of course only the positive numbers will add up, so those have to equal 100)
Copy linkTweet thisAlerts:
@katasovaauthorNov 01.2005 — i found what is wrng but i dont know how to solve it...

i pass objects to the function suma...

[CODE]suma(f.compraingram.value, f.comprasynnex.value, f.compramexmal.value, f.compracentel.value, f.compramaps.value, f.compraotro.value)[/CODE]

if the values listed above are: suma(1,1,1,1,1,1)

I try to add the values like this...

[CODE]function suma(uno, dos, tres, cuatro, cinco, seis){
...
res = uno + dos + tres + cuatro + cinco + seis;

[/CODE]


if i print 'res' res = 111111

this means that the operation in res is not making an add but joining the variables...

This was erally hard for me to explain, i hope i was clear enough....

Any idea on how to solve it?
Copy linkTweet thisAlerts:
@katasovaauthorNov 01.2005 — ok i got it how to explain it more easy....

Javascript think that the variables im passing to my function 'suma()' are text not numbers, since i got it from a text field of html

any idea on how to convert to numbers?
Copy linkTweet thisAlerts:
@konithomimoNov 01.2005 — If they are from a text box then they are sent as strings, thus they are concatenated instead of being added mathematically. You ahve to use parseInt() or eval() to make them be seen as integers.
<i>
</i>function suma(uno, dos, tres, cuatro, cinco, seis){
if(uno.length &lt; 1)
uno = 0;
if(dos.length &lt; 1)
dos = 0;
if(tres.length &lt; 1)
tres = 0;
if(cuatro.length &lt; 1)
cuatro = 0;
if(cinco.length &lt; 1)
cinco = 0;
if(seis.length &lt; 1)
seis = 0;

res = 0;
res = parseInt(uno + dos + tres + cuatro + cinco + seis);

if(res == 100)
return true;
else
return false;

}
Copy linkTweet thisAlerts:
@katasovaauthorNov 01.2005 — thx a lot it worked!!!!

have a nice day ?
Copy linkTweet thisAlerts:
@konithomimoNov 01.2005 — same to you
×

Success!

Help @katasova spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...