/    Sign up×
Community /Pin to ProfileBookmark

Javascript problem with alert box

I have this pice of code:

[CODE]
function validator() {
var l1 = document.calculation.l1.value;
var l2 = document.calculation.l2.value;
var right = document.calculation.right.value;

if (l1 == null || l1 == “”) alert(“error “);
else if (l1 == null || l2 == “”) alert(“error”);
else if (l1 == null || right == “”) alert(“error”);
else if (right == 1) { alert(“Correct”); }

else { alert(“Incorrect incorect”); }

return false;
};[/CODE]

In Firefox is ok, but in Chrome the alert box is displayed before pressing the Submit button from my form. What can be wrong here?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJul 12.2012 — What does your <form> tag look like?

Might need to be
<i>
</i>&lt;form name="myForm" action="???" method="post" onsubmit="return validator()"&gt;


Might try changing the following (I'm not sure I understand your logic or needs here):
<i>
</i>var flag = true;
if (l1 == null || l1 == "") { flag=false; alert("error "); }
if (l2 == null || l2 == "") { flag=false; alert("error"); }
if (right == null || right == "") { flag=false; alert("error"); }
if ((flag == true) &amp;&amp; (right == 1)) { alert("Correct"); }
if (flag == false) { alert("Incorrect incorect"); }

return flag;
×

Success!

Help @RedPitbull 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.20,
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,
)...