Click to See Complete Forum and Search --> : onclick works, onsubmit doesn't


ijsman
12-11-2002, 04:31 AM
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.

Rick Bull
12-11-2002, 05:15 AM
Have you tried: <FORM onsubmit="return CheckCharacters(this)"> (no .form bit)?

ijsman
12-11-2002, 06:00 AM
that did the trick thanks!

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