/    Sign up×
Community /Pin to ProfileBookmark

Break out of Javascript validation …

I have a list of validations on the save event of a form…

but when :

f.myval2.value !== f.myval3.value

is true i want to stop tha javascript and get them to do something else…

But it keeps failing by trying to proceed through the validation.
I have put a Break command in but am being told its invalid…

any help appreciated…

thanks

if (f.myval2.value !== f.myval3.value) {
break;
alert (“You must complete the info form before continuing….”);

return false;
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 03.2009 — No, breaks are for loops and switch. Use return.
Copy linkTweet thisAlerts:
@DelanJun 04.2009 — Er... you can't use return before the alert statement... well you can, but the alert call will never happen. Use "return;" after "alert(...)".
Copy linkTweet thisAlerts:
@Declan1991Jun 04.2009 — Er... you can't use return before the alert statement... well you can, but the alert call will never happen. Use "return;" after "alert(...)".[/QUOTE]

I was under the impression that he wantedif (f.myval2.value !== f.myval3.value) {
alert ("You must complete the info form before continuing....");
return false;
}
ORif (f.myval2.value === f.myval3.value) {
return true;
}
alert ("You must complete the info form before continuing....");
return false;
But he thought that break could be used in an if statement. Actually, there is a "horror story" about a break being used incorrectly to break out of an if, breaking out of the outer loop and causing chaos. Moral of the story, never use a break unless you are absolutely sure it is breaking out of the loop you want.
×

Success!

Help @stirnpanzer 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.17,
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,
)...