views:

195

answers:

2

I'm trying to add custom validation to a dynamically created control. Can I use .attr() to set a rule for a control?

$.fn.addValidationExpression = function(field) {

    if (field.ValidationExpression != null) {

        $("#fld"+getFieldIdSuffix(field)).attr("validateExpression", field.ValidationExpression);

    }

    return this;
}

What I am trying to avoid is having to do something like this:

$("#TestForm").validate(
        {
            rules: 
            {
                someControl: 
                {
                    validateExpression: true
                },
                someOtherControl: 
                {
                    validateExpression: true
                }
            }
        });
+2  A: 

Its better to use the data() option of jquery

bendewey
A: 

You probably want to do a this.each() in your $ extension as well

Scott Evernden
I have it called in a this.each() which adds the controls.
Jon Ownbey