2008年12月11日星期四

在使用 jQuery 的validate中的 addMethod方法中,遇到过的问题解决方法一

step1:
 
      $.validator.addMethod("isSameEmail",function(str1, str2){
            alert("testing....");
            alert("str1 is " + str1 + "\nstr2 is " + str2);
            return false;
        }, "The email has be registred!");

step2:
       $("#medForm").validate({
                        rules: {
                                medication : "required",
                                dosage: "required",
                                startDate : "date",
                                endDate : {date : true, isSameEmail:["this is","a test"] }
                        },

step3:
   html:>>   class="isSameEmail"



===============

Hello, I am having a little problem understanding the addMethod
function of the validation plugin.  I am trying to do a simple test
like this:

I've placed this code outside of the $().ready():

JQuery.validator.addMethod('testMethod', function(str1, str2) {
        alert("testing....");
        alert("str1 is " + str1 + "\nstr2 is " + str2);
        return false;

}, 'some message');

and this inside of $().ready():

$("#medForm").validate({
                        rules: {
                                medication : "required",
                                dosage: "required",
                                startDate : "date",
                                endDate : {date : true, testMethod : {"this is", "a test"}}
                        },
                        messages: {
                                medication: "Please provide a medication name",
                                dosage: "Please provide your dosage",
                                startDate: "Please enter a date in mm/dd/yyyy format<br/>or select
one from the calendar",
                                endDate: "Please enter a date in mm/dd/yyyy format<br/>or select
one from the calendar"
                        }

});

Here I am trying to apply the testMethod validation to the endDate
field.  When I use the form, the other validation works, but the
testMethod function is never called (no alerts are seen and the error
message never shows up under the field [the method is set to return
false no matter what]).  I tried to look at the documentation for this
on the JQuery website, but there are no examples of how to attach a
custom method to a form field..

=====================

I see at least error in your code, maybe more:

endDate : {date : true, testMethod : {"this is", "a test"}}
That isn't a valid object literal. Either replace it with an array:
["this is", "a test"] or fix it: {"this is": "a test"}

Also, you are referencing "JQuery", unless acutally defined in your
code, that should be "jQuery".

If that doesn't help, please post a testpage.

没有评论: