/    Sign up×
Community /Pin to ProfileBookmark

Improving function

The following function will check if at least 3 options have been checked in form and prompt if not.
But after prompting it goes to the page where submit button points instead staying in form and if condition is OK, it closes window instead going to where submit button points.
Can somebody fix it, I’m enough lost messing with PHP to start now flirting with javascript…

function Goingout(){

var count=0;
for (var i = 0; i <document.mainform.elements.length; i++){
if (document.mainform.elements[i].type==’radio’){
if (document.mainform.elements[i].checked==true){
count++;
}
}
}
if (count>=3) window.close()

else alert(‘Select at least two options to continue’)

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 19.2004 — Give a return check to avoid submit:

function check(){

if(codition notfulfiled){return false}

else{return true}

}

or backwords if statement, whichever you feel...

and in body

<form onsubmit="return check()">
Copy linkTweet thisAlerts:
@PerfidusauthorJan 19.2004 — I have already an event "onsubmit" on my sumit button that calls the function I wrote before, I do not know what to do with your info!?!?!
Copy linkTweet thisAlerts:
@AdamBrillJan 19.2004 — Try this instead:

function Goingout(){

var count=0;

for (var i = 0; i <document.mainform.elements.length; i++){

if (document.mainform.elements[i].type=='radio'){

if (document.mainform.elements[i].checked==true){

count++;

}

}

}

if (count>=3){ return true;

}else{

alert('Select at least two options to continue');

return false;

}

}
Copy linkTweet thisAlerts:
@Khalid_AliJan 19.2004 — Hey Adam,

you stole my desktp picture for your avtar..?
Copy linkTweet thisAlerts:
@ExuroJan 19.2004 — &nbsp; &nbsp;Yeah, Adam has the right idea with that function. Well, Kor did too but you didn't understand what he was saying... Anyway, use Adam's function and then have your form tag look something like this:

[FONT=courier new]<form onsubmit="return Goingout();">[/FONT]

&nbsp; &nbsp;Notice how Adam added a [FONT=courier new]return true;[/FONT] to the end of the "good" conditional branch, and a [FONT=courier new]return false;[/FONT] to the "bad" conditional branch. Now the function will come back and say "true" if the user has done what you want, and "false" if they haven't.

&nbsp; &nbsp;Now, the form tag does that same thing. The [FONT=courier new]onsubmit[/FONT] event will return true (because[FONT=courier new]Goingout()[/FONT] returned true) if the user has done what you want. If the [FONT=courier new]onsubmit[/FONT] event returns true, then the form submits. However, if the user has done something wrong and [FONT=courier new]onsubmit[/FONT] returns false (because [FONT=courier new]Goingout()[/FONT] returned false), then the form will not submit. Hope that makes sense!
Copy linkTweet thisAlerts:
@AdamBrillJan 19.2004 — [i]Originally posted by Khalid Ali [/i]

[B]Hey Adam,

you stole my desktp picture for your avtar..? [/B]
[/QUOTE]
Yup, that's right. ? I had a picture of The One(movie by Jet Li) as my desktop, but my sister came in and changed it to that pic of Aragorn... I ended up switching it to Frodo, though, since I liked the picture better... ?
Copy linkTweet thisAlerts:
@Khalid_AliJan 19.2004 — I had kelly Hue's picture on my desk top for a brief period of time,but ...sigh....married man can't afford that luxury,hence picture was removed from desktop in matter of minutes....:rolleyes:
Copy linkTweet thisAlerts:
@AdamBrillJan 19.2004 — [i]Originally posted by Khalid Ali [/i]

[B]I had kelly Hue's picture on my desk top for a brief period of time,but ...sigh....married man can't afford that luxury,hence picture was removed from desktop in matter of minutes....:rolleyes: [/B][/QUOTE]
LOL... Male desktop pictures are normally safer. ?
Copy linkTweet thisAlerts:
@KorJan 20.2004 — 
I have already an event "onsubmit" on my submit button that calls the function I wrote before
[/quote]


No one , except Exuro, was attentive to what I said.

The onSubmit event handler handles the FORM, not the INPUT. If use onSubmit in an input button you may fire a function, but you may not stop the submit action. So, I repeat, the onsubmit should be place in FORM, not in INPUT submit button.

<form onsubmit="return blablafunction()">

NOT

<input type="submit" onsubmit.....>
Copy linkTweet thisAlerts:
@AdamBrillJan 20.2004 — [i]Originally posted by Kor [/i]

[B]No one , except Exuro, was attentive to what I said.[/B][/QUOTE]
I knew exactly what you meant, but since you decided not to give him the code that he needed but only show him what to do, I gave him what should be the exact code needed. I did not, however, post the form because I was in a bit of a hurry when I posted that, but Exuro was nice enough to show how to use my code...
Copy linkTweet thisAlerts:
@KorJan 20.2004 — Ok, OK, no offence, I guess... The main ideea is that we all hope that we helped Perfidus solve his problem ?
×

Success!

Help @Perfidus 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.12,
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,
)...