开发者

jQuery Validation on field(s) with a prefix in the name property

I'm trying to add the jQuery Validation plugin to some websites and I'm running into a bit of an issue in that the fields that it's supposed to validate have a prefix on t开发者_JAVA百科he name property, such as "Customer.FirstName".

Since you have to pass a JSON object to the validate function for the rules, it doesn't work because it never finds the elements.

Is there a way to do per field basis, or can I still pass in a variant of the JSON object that specifies the field id as a string, such as "#Customer\.FirstName"?

Thanks in advance!

EDIT:

Per Greg's suggestions, I got it to work. So for anyone who has issues like these, you have to do it like this:

$("form").validate({
    rules: {
        "Prefix.FieldName": "validationKeyword"
    }
});

DO NOT add the "#" to the selector, and DO NOT add the "\\" escape chars to the selector. So, "#Prefix\\.FieldName" will not match anything, so just leave those chars out.


JSON supports keys with "." in them - just quote them:

var obj = {"#Customer.FirstName": "value"};

In fact to be proper JSON they should always be double-quoted.

Edit: if this is a selector then you can escape the . like this: "#Customer\\.FirstName"

Edit2: The docs say the key needs to be a name so I it should either be "Customer.Firstname" or "Customer\.Firstname" depending on how well-coded the plugin is. You'll need <input name="Customer.Firstname" ...>.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜