First of all, I apologize if I have put this topic in wrong category, but I am new to Drupal and this is my first post.

Does anyone know how can we stop form from submitting and do client-side validation? We need to be able to have JavaScript confirmation before submitting, but currently form does submit either we have chosen YES or CANCEL on confirm. Thans in advance.

Comments

nevets’s picture

Your javascript submit handler should return false on validation errors (or user clicking cancel).

srdrupal’s picture

Hello and thanks for your answer.
But, we have done that, we returned false from our submit handler and still it didn't work, Drupal submits our form. Is there anything else that might be a solution to our problem? Thanks a lot!

nevets’s picture

So without knowing your code it hard to even know where to start. I would start by using firebug (and extension to firefox) and see what the validation code is doing. Also, are you attaching the submit handler to the form or the button? (I am pretty sure you want it attached to the form).

fayola’s picture

say I add to the submit button

$form['submit']['#attributes'] = array('onclick'=>'confirm("testing...")';

How do I stop the from from submitting if confirm returns false???? It seems to go through even it I click cancel.

~fayola
--------
http://www.fayola.net

nevets’s picture

I think you need to onsubmit in order to abort the form being submitted.

fayola’s picture

tried:

$form['submit']['#attributes'] = array('onsubmit' => 'confirm("testing...")');

and

$form['#attributes'] = array('onsubmit' => 'confirm("testing...")');

still no luck.

~fayola
--------
http://www.fayola.net

fayola’s picture

so I guess no one has any ideas?

I don't want to have to add a new module (javascript validation one it think) as this is a production site I'm woring on and I want to to be as minimal as possible

~fayola
--------
http://www.fayola.net

dwees’s picture

Can you pastebin the html result of your form? Ie, show us what Drupal is producing?

Dave

My site: http://www.unitorganizer.com/myblog

zigma’s picture

$form['#attributes']['onsubmit'] = 'return my_js_validate_function()';

in your javascript function return false if it fails validation.

fayola’s picture

thanks for the suggestion, I can see this working, but I had already came up with an alternative. I will keep this in mind for the future.

~fayola
--------
http://www.fayola.net

zahor’s picture

Hey, can you mention what the alternative is?