Form Validation using Prototype(no return false): How to cancel an event

I remember getting lost a few times back when I first started using Prototype. I was trying to implement some form validation, which is pretty easy with prototype, but ‘old habits’ were getting in my way. Coming from ‘plain old javascript’, I was accustomed to using ‘return false;’ to cancel a form submission, but when you’re trying to separate form from function(and thats really the whole point of events), you don’t necessarily want to put javascript into your html. e.g.:

<form onsubmit=’return myValidator();’>

First of all, it’s ugly. More importantly though, it mixes your javascript and your html, if you want to rename your validator function you now have to dip into the HTML code(which should probably live in a different file)… ew, divs. Since you’re using prototype you should be using prototype’s event management. Here’s a sample:

<form id=”myForm”…>

$(’myForm’).observe(’submit’, doValidation);
function doValidation(event) {
//do validation here
}

And, in order to stop the form from submitting you would use Prototype’s Event library again:

myEvent.stop();

This stops the submission event from ‘bubbling up(or down)’ to the Document Object and being submitted. This is our ‘return false;’

Putting it all together we get something like this:

<form id=”myForm”…>
<input id=”email”

$(’myForm’).observe(’submit’, doValidation);
function doValidation(submissionEvent) {
if($(’email’) && !(/^.+@.+\..+/.exec($(’email’).value))){
//not valid, let the user know.
submissionEvent.stop();
}
//do more validation here
}

..and there was much rejoicing. Our HTML and and javascript are separate, our lives are easier. Our new code also makes it clear that form submissions are actually events and that our validation function is a handler for that event.
Cheers!

Apparenly Ryan Campbell wrote on the same subject back in 2006, he claimed that Event.stop() didn’t work in Safari but as far as I know that’s no longer true.
Prototype’s api entry for event.stop.

Tags: , ,

68 Responses to “Form Validation using Prototype(no return false): How to cancel an event”

Dustin Graham Says:

Thanks man! This is exactly what I was looking for. I was trying all forms of Event.observe(id, ‘click’ or ’submit’) with functions return false! But, I was looking for event.stop(); thanks.

FREDDIE Says:

Buy@Cheap.Coral.Calcium” rel=”nofollow”>…

Buywithout prescription…

PERRY Says:

Purchase@Coral.Calcium.Online” rel=”nofollow”>…

Buywithout prescription…

OTIS Says:

Purchase@Abana.Online” rel=”nofollow”>..

Buydrugs without prescription…

LEO Says:

Purchase@Cheap.Abana” rel=”nofollow”>…

Buygeneric pills…

EDUARDO Says:

Cheap@Generic.Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>..

Buygeneric pills…

WALLACE Says:

Get@Acai.Online” rel=”nofollow”>…

Buygeneric drugs…

BRANDON Says:

Cheap@Coral.Calcium.Online” rel=”nofollow”>..

Buyno prescription…

SALVADOR Says:

Buy@Discount.Abana” rel=”nofollow”>..

Buygeneric meds…

TERRENCE Says:

Order@Discount.Abilify” rel=”nofollow”>..

Buyit now…

LEWIS Says:

Cheap@Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>..

Buynow it…

EDUARDO Says:

Buy@Cheap.Abilify” rel=”nofollow”>.

Buynow it…

ROY Says:

Buy@Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>…

Buywithout prescription…

RICARDO Says:

Buy@Cheap.Acai” rel=”nofollow”>..

Buydrugs without prescription…

FELIX Says:

Buy@Discount.Acai” rel=”nofollow”>…

Buyit now…

TRACY Says:

Order@Cheap.Acai” rel=”nofollow”>…

Buywithout prescription…

WILLIE Says:

Order@Cheap.Acai” rel=”nofollow”>…

Buygeneric drugs…

SALVADOR Says:

Order@Discount.Acai” rel=”nofollow”>…

Buynow…

JEFFREY Says:

Purchase@Acai.Online” rel=”nofollow”>…

Buynow it…

ALVIN Says:

Cheap@Acai.Online” rel=”nofollow”>..

Buynow it…

RUSSELL Says:

Buy@Generic.Acai” rel=”nofollow”>..

Buywithout prescription…

JUAN Says:

Buy@Generic.Acai” rel=”nofollow”>..

Buydrugs without prescription…

BILLY Says:

Cheap@Generic.Aciphex” rel=”nofollow”>.…

Buygeneric drugs…

ADAM Says:

Order@Generic.Actonel” rel=”nofollow”>.…

Buyno prescription…

RAY Says:

Purchase@Cheap.Actoplus.Met” rel=”nofollow”>.

Buygeneric drugs zfk…

LLOYD Says:

Purchase@Discount.Actos” rel=”nofollow”>…

Buywithout prescription xiu…

JEREMIAH Says:

Cheap@Actos.Online” rel=”nofollow”>..

Buynow it tly…

CLIFTON Says:

Cheap@Generic.Actos” rel=”nofollow”>..

Buygeneric meds qmo…

VICTOR Says:

Purchase@Generic.Actos.15mg.30mg” rel=”nofollow”>..

Buygeneric pills zdg…

EVERETT Says:

Purchase@Cheap.Adalat” rel=”nofollow”>..

Buygeneric meds abx…

MARCUS Says:

Purchase@Cheap.Coral.Calcium” rel=”nofollow”>..

Buynow it vhx…

MARVIN Says:

Order@Acai.Without.Prescription” rel=”nofollow”>.

Buynow it lmg…

JOHNNY Says:

Order@Generic.Accutane” rel=”nofollow”>…

Buygeneric meds svb…

JOEL Says:

Buy@Discount.Aciphex” rel=”nofollow”>..

Buyno prescription wkp…

JERRY Says:

Purchase@Aciphex.Online” rel=”nofollow”>…

Buydrugs without prescription vrg…

GLENN Says:

Buy@Aciphex.Without.Prescription” rel=”nofollow”>..

Buyno prescription tao…

VINCENT Says:

Purchase@Cheap.Acomplia” rel=”nofollow”>.

Buyit now ojb…

VERNON Says:

Purchase@Discount.Acomplia” rel=”nofollow”>…

Buynow ela…

NICHOLAS Says:

Buy@Actonel.Online” rel=”nofollow”>.

Buygeneric pills oir…

FREDRICK Says:

abilify@and.gemfibrozil” rel=”nofollow”>.

Buyno prescription…

MAX Says:

amantadine@hydrochloride.tablets” rel=”nofollow”>..

Buygeneric drugs…

RAFAEL Says:

arjuna@pills.buy” rel=”nofollow”>..

Buygeneric meds…

MIKE Says:

viagra order uk

Buy_generic pills…

DUSTIN Says:

weight loss disclaimer law

Buy_drugs without prescription…

CHRIS Says:

cancer of spine

Buy_without prescription…

JOEL Says:

how to get teens off drugs

Buy_drugs without prescription…

KURT Says:

can a woman have colorectal cancer

Buy_drugs without prescription…

Leave a Reply