Hi,

Thanks for the jQuery plugin module. Great idea.

I'm new at Drupal and jQuery...could someone post a usage example to enable form field validation in a custom module, please?

Thanks, mitz

Comments

mitz’s picture

PS I'm assumed, probably incorrectly, that the 1.1 from the version number on this request is for 1.1.1 version of jquery.validate.min.js. Anyway, I just downloaded jquery_update and jquery_plugin from the Drupal site yesterday (22 June 2008).

kevinquillen’s picture

Webforms module makes use of this, by the way. (If youre so inclined to use it)

mfb’s picture

Here's a straight-forward usage; just loading your validation rules from a javascript file:

function mymodule_user($type, &$edit, &$user) {
  if ($type == 'register') {
    jquery_plugin_add('validate');
    drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');
  }
}

Put the rules in the module's javascript file:

$(function() {
  $("#user-register").validate({
    rules: {
      myfield1: {
        minLength: 5,
        required: true
      },
      myfield2: {
        required: true
      },
      myfield4: {
        required: function() {
          return $("#myfield3").val() < 1;
        }
      }
    }
  });
});

There are many more advanced usage possibilities, for example using the metadata plugin to store your validation rules as HTML attributes; etc.

mitz’s picture

Thanks, mfb. I'll give it a try.

Also, thanks gh0st25 for the info.

mfb’s picture

Status: Active » Fixed

Please re-open if you have other questions.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

LEternity’s picture

Status: Closed (fixed) » Active

I'd like to know whether I could use this with the editablefields module. If so, would you be able to provide a simple example of how to implement this? Thank you!

bala.d’s picture

Use the js file as follows:
$(function() {

$("#user-register").validate({

rules: {
'name': {
minlength: 5,
required: true
},
'mail': {
minlength: 10,
required: true
},
}
});
});

Be careful. as simple typo error wont give results...
Thanks to mfb...

umesh_chaudhari’s picture

hi where do i need to put this code

<?php
function mymodule_user($type, &$edit, &$user) {
  if ($type == 'register') {
    jquery_plugin_add('validate');
    drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');
  }
}
?>
hiramanpatil’s picture

Version: 5.x-1.1 » 7.x-1.0