How do I add client-side form validation to a Drupal form? For example, in the comment module. If the vistor don't enter the email address, I want to popup a window that says: "please enter the email address".

I hope someone can give me an example of how I do this.

  • Thanks to Attiks, I solved my major problem through this jquery validation. – user5232 Jan 28 '12 at 11:32
  • I think it is easy to just include the Jquery Validation js file in our custom module or theme and then add js validation rules for the form. No extra load with any module. – shasi kanth Oct 21 '14 at 8:57

You can either try the Clientside Validation module ( I haven't ) or you can handle it via jQuery required plugin for specific form elements that you require by adding validation rules in your custom js file and loading it with your custom theme - http://docs.jquery.com/Plugins/Validation/Methods/required there is no perceivable Drupal form API quirks that you need to handle as its all on the client side.

  • if i want to write the js code by myself, how to do it? thank you. – enjoylife Mar 18 '11 at 13:50
  • best way to do it imo would be to create a new js file in your custom theme and loading it in the theme info file - adding this to your theme info should do it - scripts[] = custom_js.js – Dipen Mar 18 '11 at 13:53
  • Look at - drupal.org/node/304255 I assume you are using d6 – Dipen Mar 18 '11 at 13:57
  • thank you, but i don't know how to add the function(onclick, on submit,...which may be used in the js file) to the form that i want to do a client-side form validation. – enjoylife Mar 18 '11 at 14:05
  • if you use the jquery plugin I linked to above, you don't have to write any onclick, onsubmit event you need to include the plugin js file + you need to include your custom js file with rules. Check an example at docs.jquery.com/Plugins/Validation/Methods/required before trying it with drupal. – Dipen Mar 18 '11 at 14:08

I'm biased since i created http://drupal.org/project/clientside_validation, but it is using http://bassistance.de/jquery-plugins/jquery-plugin-validation/ to do all validations, the benefit of the module is that you don't have to write any code


Use a preprocess function to load http://bassistance.de/jquery-plugins/jquery-plugin-validation/ and a script that has specific settings for your node form.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.