【问题标题】:Regular Expression jQuery validation正则表达式 jQuery 验证
【发布时间】:2010-06-25 14:09:50
【问题描述】:

我的任务是在我已经实现的表单验证上应用正则表达式(使用 jquery.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.任何帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: jquery html css validation expression


    【解决方案1】:

    如果您在配置验证和添加规则时使用 jquery.validate.js,请尝试以下方式(添加您自己的规则):

    $('#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
                    }
                }
            }
        }
    });
    

    如果我正确理解你的问题,这样的事情应该会让你朝着正确的方向前进。

    【讨论】:

      【解决方案2】:

      您可以添加逻辑并根据所选内容调用验证方法。或者,您可以创建一个 jQuery UI 小部件工厂来跟踪要使用的验证。不过,这可能有点矫枉过正。

      【讨论】: