开发者

Regular Expression jQuery validation

I have been given the task to apply regular expressions on a form validation which I have achieved (using j开发者_开发知识库query.validate.js). Now, I have to apply a type of regular expressions when the select tag is pointed to US and other type of regular expressions when is pointed to any other country. Any help will be fully appreciated. Thanks in advance.


If you're using jquery.validate.js when you configure validate and add your rules, try something along the lines of (to add your own rule):

$('#myForm').validate({
    rules: {
        'myField': {
            myRule: function(element) {
                var option = element.find('option:selected');
                if (option.val() == 'US') {
                    return // The result of your US only regular expression check
                } else {
                    return // The result of your other regular expression check
                }
            }
        }
    }
});

Something like that should get you in the right direction if I understand your question correctly.


You could either add logic and call a validation method based on what is selected. Or, you could create a jQuery UI widget factory that tracks which validation is to be used. That may be a little overkill for this though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜