Link to home
Create AccountLog in
Avatar of dba123
dba123

asked on

JQuery AddMethod questions on function(value, element, params)

I can't for the life of my find some decent information on how to pass an array of values from my .aspx to this addMethod that I created.

function(value, element, params)
http://jquery.bassistance.de/api-browser/plugins.html#jQueryformatStringArrayString

So lets say I have this in my .aspx View:

<script type='text/javascript'>

$(document).ready(function()
{
    $("#TestForm").validate
        ({
            rules: {
            compareAddresses: {mytestvalidatemethod: "myAddress1,myAddress2"}
           }
        });
 });
 
 </script>

and this is part of my addMethod I added to our jquery.validate.addons.js:

jQuery.validator.addMethod("mytestvalidatemethod", function(value, element, params) {
if (jQuery(params[0]).val() == jQuery(params[1]).val())
                 {
...
}

I want to pass in the 2 fields in for the params array so I can split them out and do something with those two.  I know I can grab them in the array just like any other array by doing a params[0] or params[1] but I just don't know how to pass them from my .aspx
Avatar of hielo
hielo
Flag of Wallis and Futuna image

NOTE: "mytestvalidatemethod" expects three arguments. This:
compareAddresses: {mytestvalidatemethod: "myAddress1,myAddress2"}
seems like you are trying to pass just one.

At any rate, since mytestvalidatemethod() is already a function, you just need to pass it the parameters directly, similar to the following:
compareAddresses: mytestvalidatemethod( "1st Street",document.form1.address, ["myAddress1,myAddress2"]}

the first argument being "1st Street
the second, a reference to an input field named address
the thirt, an anonymous array

I've don't use jQuery, but from what I am seeing you probably need to prefix the method with jQuery.validator:
<script type='text/javascript'>

$(document).ready(function()
{
    $("#TestForm").validate
        ({
            rules: {
            compareAddresses: jQuery.validator.mytestvalidatemethod( "1st Street",document.form1.address, ["myAddress1,myAddress2"])
           }
        });
 });
 
 </script>

which would call the method immediately and rules.compareAddress will be assigned whatever jQuery.validator.mytestvalidatemethod returns.
<script type='text/javascript'>

$(document).ready(function()
{
    $("#TestForm").validate
        ({
            rules: {
            compareAddresses: function(a,b,c){ return jQuery.validator.mytestvalidatemethod( a,b,c );}
           }
        });
 });
 
 </script>
Avatar of dba123
dba123

ASKER

No, the first two arguments are passed automatically by JQuery.
Avatar of dba123

ASKER

JQuery is a JavaScript framework.  Please see the link.
Avatar of dba123

ASKER

you did answer my question though which is how to pass the values to my params which is ["myAddress1,myAddress2"]
Avatar of dba123

ASKER

but I don't actually think ["myAddress1,myAddress2"] is correct in the JQuery language or is this just pure JavaScript?
Avatar of dba123

ASKER

I think i have to pass it this way:
["myAddress1","myAddress2"]
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of dba123

ASKER

This is the correct way to pass it:

["myAddress1","myAddress2"]

resolved.
Avatar of dba123

ASKER

and then now I can access each by doing a params[0], params[1], etc.
Avatar of dba123

ASKER

was close but you answered some of my other questions that I didn't really post in here so thanks.