views:

1026

answers:

6

If I've added a custom validation method to my form, how can I get it to trigger prior to submit?

I'm not decorating my form fields but I thought that adding the new method to my rules section would trigger the method but it doesn't work.

$.validator.addMethod('myCustomValidation', function (data)
{
   // do some work here 
   return myValidationResult;

}, 'Please respond.');


$("#myFormId").validate({        
    rules: {
         "myCustomValidation": {required: true}
    },
    messages: {
       "myCustomValidation": {
           required: "Enter some information before proceeding",        
        }
    },
    submitHandler: function(form)
    {
        // do the work to submit this form
    }
});

I'd like have the validation get triggered when the 'submit' button is pressed.

However, if I add the validation class name to any form field except the submit button, this rule gets triggered. Just can't seem to get it to be triggered just prior to submit..

A: 

wrap your code in:

$(function(){
   //your code goes here
});

this will attach validation events on-page-load.

rochal
It's already wrapped -- it's more that the validation doesn't get triggered prior to the form being submitted.
Robin Jamieson
A: 

I haven't used the addMethod function but I can suggest you this:

$.validator.methods.myCustomValidation = function(value, element, param) {
    // Perform custom validation and return true or false
    return value === '' || /^([0-9]{10})$/.test(value);
};


$("#myFormId").validate({        
    rules: {
        myElement: { 
            myCustomValidation: true 
        }
    },
    messages: {
        myElement: { 
            myCustomValidation: "Enter some information before proceeding" 
        }
    },
    submitHandler: function(form)
    {
        // do the work to submit this form
    }
});
Darin Dimitrov
I tried this but it doesn't seem to work. What I am trying to do is make sure the validation gets fired before the submitHandler is invoked. I thought having the rules on the element (specified in class name) would trigger the validation.. Perhaps I don't understand the docs all that well.
Robin Jamieson
A: 

You are missing something. rules should look like this.

rules: {
    "myCustomValidation": {
        required: true,
        myCustomValidation: true
     }
},
jitter
A: 

Call .valid() to trigger the validation:

$("#myFormId").valid();
Chris Pebble
Can you call 'valid' inside the submitHandler?
Robin Jamieson
A: 

I think you're looking for addClassRules

$.validator.addMethod('myCustomValidation', function (data)
{
   // do some work here 
   return myValidationResult;

}, 'Please respond.');

$.validator.addClassRules({
  myCustomValidation: { myCustomValidation: true, required: true }
});

$("#myFormId").validate({        
    messages: {
       "myCustomValidation": {
           required: "Enter some information before proceeding",        
        }
    },
    submitHandler: function(form)
    {
        // do the work to submit this form
    }
});
Kyle Butt
A: 

the jquery path is missing

constantine