TypeError:无法获取属性的值' test&#39 ;: object为null或undefinedundefined

时间:2015-10-27 19:44:17

标签: javascript angularjs internet-explorer internet-explorer-8

我有一个通过一组正则表达式计算字符串的函数。这些正则表达式在charRules内包装为自己的对象。

如果字符串无效,则返回false,否则返回true。

示例有效字符串:1234567890123K

示例无效字符串:!@#$%^&*!!!&H5

当用户在文本输入中输入值时,IE8会在控制台中输出错误

TypeError: Unable to get value of the property 'test': object is null or undefinedundefined

IE9 +,Chrome,Firefox,Safari按预期工作。

逻辑作为指令实现。这是附加到范围的核心逻辑,

                    scope.validate = function(value) {

                        // Letters and special characters not allowed per country.
                        var charRules = {
                            br: {
                                haveLetters: /[a-zA-Z]/,
                                haveSpecials: /[!@$%^&*()_+|~=`\\#{}\[\]:";'<>?,]/,
                                minMaxLength: /^.{12,25}$/
                            },
                            cl: {
                                haveLetters: /[a-jl-zA-JL-Z]/,
                                haveSpecials: /[!@$%^&*()_+|~=`\\#{}\[\]:";'<>?,\/]/,
                                minMaxLength: /^.{12,25}$/
                            },
                            mx: {
                                haveLetters: /[]/,
                                haveSpecials: /[!@$%^&*()_+|~=`\\#{}\[\]:";'<>?,\/.-]/,
                                minMaxLength: /^.{12,25}$/
                            },
                            pr: {
                                haveLetters: /[a-zA-Z]/,
                                haveSpecials: /[!@$%^&*()_+|~=`\\#{}\[\]:";'<>?,\/.]/,
                                minMaxLength: /^.{12,25}$/
                            }
                        };

                        if (charRules[country]) {
                            if (charRules[country].haveLetters.test(value) || charRules[country].haveSpecials.test(value) || !charRules[country].minMaxLength.test(value)) {
                                return false;
                            } else {
                                return true;
                            }
                        }
                    };

country变量是全局定义的。适用于HTML的指令是rut="mx"

它适用于此HTML,

<input type="text" id="address_rut" rut="mx" class="input-xlarge" ng-switch-when="mx" ng-model="rutnumber.taxIDNumber" ng-show="editing" required>

有关导致字符串值不在IE8中解释的原因的任何想法?

2 个答案:

答案 0 :(得分:1)

Internet Explorer 8抛出:

  

正则表达式中的预期']'

尝试在正则表达式中使用空括号时。

    /[]/

您应该尝试更加具体地使用您不想在其中匹配的字符 类似的东西,根据您的需要,您可能需要添加更多不包括条件:

/[^\w^\w]/

答案 1 :(得分:0)

在上述if条件下,尝试

null!=charRules[country].haveLetters.exec(value) || null!=charRules[country].haveSpecials.exec(value) || (value.length>12 && value.length<25)

因为javascript test()在一些旧版本的IE中不起作用。

相关问题