/    Sign up×
Community /Pin to ProfileBookmark

onclick works, onsubmit doesn’t

Ok i’ve been struggling with this script all day and i can’t figure out how to fix this.

The idea is that i have a form with a name field. this field can only contain letters.

this is my script:

<script language=”javascript”>

function CheckCharacters(thisform) {

var str = thisform.naam.value
var slen = str.length
for (i=0; i < slen; i++) {
var ch = str.substring(i,i+1)
if ( (ch < “a” || “z” < ch) && (ch < “A” || “Z” < ch) && ch != ” “) {
alert(“Teken ” + (i+1) + ” in de naam is niet toegestaan”)
thisform.naam.focus()
return false
}
}

}

</script>

my form looks like this:

<FORM >
<INPUT TYPE=”text” SIZE=”35″ MAXLENGTH=”20″ NAME=”naam” VALUE=”” >

<INPUT TYPE=”submit” VALUE=”Volgende” onclick=”return CheckCharacters(this.form)”>
</form>

this works, but only if i click the button, if i hit enter it still submits the form. so i try this:

<FORM onsubmit=”return CheckCharacters(this.form)”>
<INPUT TYPE=”text” SIZE=”35″ MAXLENGTH=”20″ NAME=”naam” VALUE=”” >

<INPUT TYPE=”submit” VALUE=”Volgende” >
</form>

but this doesnt work at all! I get some errors and the form always submits.

What am i doing wrong? a lot of tutorials i found only go about onclick validation thereby completely forgetting that if you press enter is stil submits.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Rick_BullDec 11.2002 — Have you tried: <FORM onsubmit="return CheckCharacters(this)"> (no .form bit)?
Copy linkTweet thisAlerts:
@ijsmanauthorDec 11.2002 — that did the trick thanks!

Still can't figure out why no tutorial explained this to me.
×

Success!

Help @ijsman 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.18,
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,
)...