views:

51

answers:

2

Im using jQuery's validation which can be found here: http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules

I currently have some of my 'custom' rules set up like so:

else if(valID =="#shipForm")
        {
            $("#fName").rules("add",{regexName: "^[a-zA-Z]+(([\'\-][a-zA-Z])?[a-zA-Z]*)*$",minlength: 2,messages:{minlength:"Must be 2 characters."}});
            $("#lName").rules("add",{regexName: "^[a-zA-Z]+(([\'\-][a-zA-Z])?[a-zA-Z]*)*$",minlength: 2,messages:{minlength:"Must be 2 characters."}});
            $("#sAdd1").rules("add",{stringCheck: "",minlength:2,messages:{minlength:"Enter your complete street address."}});
            $("#sAdd2").rules("add",{stringCheck: ""});
            $("#city").rules("add",{stringCheck: "",minlength:2,messages:{minlength:"Enter the full name of your city"}});
            $("#zipcode").rules("add",{stripZip: ""});
            $("#phoneIn").rules("add",{stripPhone: "",maxlength:15,messages:{maxlength:"Phone number exceeds allowed length"}});
            $("#altPhone").rules("add",{stripPhone: ""});
            $("#state").rules("add",{checkMenu: ""});
            $("#country").rules("add",{checkMenu: ""});
        }

What I was hoping to do.. is abstracting the .rules out and being able to grab them from a function. My issue is that they are not just strings, so im lacking an idea of how i could bring info from another function and populate the .rules("passed value of rule")

This doesnt work but this is an example of something i was kinda hoping for

function getRule(rule)
{
    switch (rule)
    {
        case "fName":
            return "\"add\",{regexName: \"^[a-zA-Z]+(([\'\-][a-zA-Z])?[a-zA-Z]*)*$\",minlength: 2,messages:{minlength:\"Must be 2 characters.\"}}";
            break;
    }
}

But I obviously cant just pass a string back and run it back into the .rules.

Any idea's?

A: 

You can store your rules in a map:

var rules = {
    fName: {
        regexName: "^[a-zA-Z]+(([\'\-][a-zA-Z])?[a-zA-Z]*)*$",
        minlength: 2,
        messages: {
            minlength: "Must be 2 characters."
        }
    },
    lName: { // ... }
    // ...
};

And apply all of them with:

for (var id in rules) {
    $("#" + id).rules("add", rules[id]);
}

Note: You don't need a break after a return.

Note2: Wrap and indent your object literals for readability.

Ates Goral
Thanks for the notes. Ill look into this method
littlevahn
A: 

You could return an object

function getRule(rule) 
{ 
    switch (rule) 
    { 
        case "fName": 
            return {
               param1 : "add", 
               param2 : {
                    regexName: "^[a-zA-Z]+(([\'\-][a-zA-Z])?[a-zA-Z]*)*$",
                    minlength: 2,
                    messages:{
                         minlength:"Must be 2 characters."
                    }
                }
            }; 
    } 
} 

now if you call getRule it will return an object like this

var myruleDef = getRule(rule);

$(selector).rules(myruleDef.param1, myruleDef.param2);
John Hartsock
Given the way it's used, maybe return an array instead of an object? that way he could just call `.rules(myruleDef)` no?
Ryley
Thanks, I think this will work best for my current set up
littlevahn