/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Prevent a form from executing action

Is there a way to prevent a form from executing it’s specified action if a condition is met?

There is a combo box in a form and if the user chooses the option that has a value of 0 I do not want the form to submit.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 27.2009 — validate if the selected option when onsubmit occur, then return false to prevent form submission:
<i>
</i>&lt;form ... onsubmit="if(this.mysel.value == '0')return false;"&gt;
&lt;select name='mysel'&gt;
&lt;option value='0'&gt;0&lt;/option&gt;
&lt;option value="..."&gt;...&lt;/option&gt;
&lt;/select&gt;
...
&lt;/form&gt;


of course, in this sample the code is wrote directly in the event but instead you can use a function.
Copy linkTweet thisAlerts:
@cfgcjmauthorApr 27.2009 — Ok, I implemented it as an external function like here:

[CODE]
function checkShipping(){
if(document.getElementById("shipper").value == 0)
{
alert("You must select a shipping method");
return false;
}

}[/CODE]


and my call is
[CODE]<form action="https://www.paypal.com/cgi-bin/webscr" onsubmit="checkShipping();" method="post" id="finalCheckoutButton">[/CODE]


I'm getting the alert box but it's not stopping the form from submitting...not sure why
Copy linkTweet thisAlerts:
@AmphidamasApr 27.2009 — Try removing the semi-colon highlighted in red.

<i>
</i>&lt;form action="https://www.paypal.com/cgi-bin/webscr" onsubmit="checkShipping()[COLOR="Red"];[/COLOR]" method="post" id="finalCheckoutButton"&gt;


EDIT: Could you also post the code for your submit button, please.
Copy linkTweet thisAlerts:
@cfgcjmauthorApr 27.2009 — nope, still goes through
Copy linkTweet thisAlerts:
@AmphidamasApr 27.2009 — Try this as your call:

<i>
</i>&lt;form action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return checkShipping()" method="post" id="finalCheckoutButton"&gt;


That should do it.
Copy linkTweet thisAlerts:
@cfgcjmauthorApr 27.2009 — Now i didn't even get the alert box...straight to paypal
Copy linkTweet thisAlerts:
@cfgcjmauthorApr 27.2009 — I lied...I updated the wrong file.

It worked great!

Thanks
Copy linkTweet thisAlerts:
@AmphidamasApr 27.2009 — I lied...I updated the wrong file.

It worked great!

Thanks[/QUOTE]


? Not a problem.

Regards,

Amph.
×

Success!

Help @cfgcjm 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.25,
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,
)...