English 中文(简体)
jQuery - Getting custom validation methods to trigger
原标题:

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..

问题回答

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
    }
});

wrap your code in:

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

this will attach validation events on-page-load.

Call .valid() to trigger the validation:

$("#myFormId").valid();

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
    }
});

You are missing something. rules should look like this.

rules: {
    "elemId": {
        required: true,
        myCustomValidation: true
     }
},

I recommend using the native $.validator.addMethod to bind the custom method. After that, just call jquery s blur() on the element to trigger validation.

Due to a race condition that I suspect exists in jQuery Validate, I had to spin wait in order to avoid getting a blank error message when I programmatically invoke validation

            //after binding custom validation method
           if($("#myElement").length){//add any checks you need
                var millisecondsToWait = 500;
                //Spin because it works
                setTimeout(function() {
                    $("#myElement").blur();
                    //$("#myElement").valid();//or this
                }, millisecondsToWait);




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签