onclick works, onsubmit doesn't

admin

Administrator
Staff member
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=http://www.webdeveloper.com/forum/archive/index.php/"" >

<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.
 
Back
Top