Forms with return

J

Joel Spolén

For some reason my form does'nt work as I wont it to do, big surprise :)

Anyway the problem is that when you click on submit you call the function
verifiera() and the function checks that everything is ok, but sadly it
continues to the next page even though I written
return false;

Don't know why, is there anyone who can say what I'm doing wrong!

here is the code

<script language="JavaScript">
function verifiera() {
if (document.gastbok.namn.value.length < 2) {
alert("Skriv ditt fullständiga namn.");
return false;
}
if (document.gastbok.textarea.value.length < 1) {
alert("Du måste skriva något för att skicka inlägget.");
return false;
}
return true;
}


</script>
 
M

Michael Winter

Joel Spolén wrote:

[snip]
adly [the form] continues to the next page even though I written
return false;


Did you return that within the event listener, too? That is,

<form ... onsubmit="return verifiera();">

[snip]
<script language="JavaScript">

The language attribute is deprecated. Use the (required) type attribute
instead:

function verifiera() {
if (document.gastbok.namn.value.length < 2) {

An easier way to reference the FORM element is to pass a reference when
you call the function:

<form ... onsubmit="return verifiera(this);">

function verifiera(form) {
if(form.namn.value.length < 2) {
/* ... */
}
}

or preferably:

function verifiera(form) {var elem = form.elements;
if(elem.namn.value.length < 2) {
/* ... */
}
}

Hope that helps,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top