开发者

jQuery validate : multiple form, single validation

开发者 https://www.devze.com 2023-02-03 14:05 出处:网络
I have 2 forms, namely (ids mentioned) \'add_product\' and \'edit_product\' Now I am using jQuery validation plug-in, both the forms have same validation (similar to mentioned in following example)

I have 2 forms, namely (ids mentioned) 'add_product' and 'edit_product'

Now I am using jQuery validation plug-in, both the forms have same validation (similar to mentioned in following example)

jQuery("#add_product").validate({
    rules: {
        name:"required",
        price:"number"
    },
    messages: {
        name:"Please enter Plan Name",
     开发者_运维知识库   price:"Enter a valid number"
    }

});

and

jQuery("#edit_product").validate({
    rules: {
        name:"required",
        price:"number"
    },
    messages: {
        name:"Please enter Plan Name",
        price:"Enter a valid number"
    }

});

I want to combine them into 1 rule/function, how can I achieve this?

Thanks


Store the options in a variable and reuse it.

var vProduct = {
    rules: {
        name:"required",
        price:"number"
    },
    messages: {
        name:"Please enter Plan Name",
        price:"Enter a valid number"
    }
};

jQuery("#add_product").validate(vProduct);
jQuery("#edit_product").validate(vProduct);

Unfortunately you can't simply use jQuery("#add_product, #edit_product").validate({...}) because the plugin doesn't handle more than one element being selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消