正则表达式不适用于IE,但适用于其他浏览器

时间:2015-03-04 14:32:15

标签: javascript jquery regex internet-explorer

(function($){
    $.fn.validationEngineLanguage = function(){
    };
    $.validationEngineLanguage = {
        newLang: function(){
            $.validationEngineLanguage.allRules = {
                "required": { // Add your regex rules here, you can take telephone as an example
                    "regex": "none",
                    "alertText": "This field is required",
                    "alertTextCheckboxMultiple": "Please select an option",
                    "alertTextCheckboxe": "This checkbox is required",
                    "alertTextDateRange": "Both date range fields are required"
                },

                "phone": {
                    // credit: jquery.h5validate.js / orefalo
                    "regex": /^([\+][0-9]{1,3}[\ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9\ \.\-\/]{3,20})((x|ext|extension)[\ ]?[0-9]{1,4})?$/,
                    "alertText": "Invalid phone number"
                },
                "email": {
                    // HTML5 compatible email regex ( http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#    e-mail-state-%28type=email%29 )
                    "regex": /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
                    "alertText": "Invalid email address"
                },
                "integer": {
                    "regex": /^[\-\+]?\d+$/,
                    "alertText": "Not a valid integer"
                },
                "number": {
                    // Number, including positive, negative, and floating decimal. credit: orefalo
                    "regex": /^[\-\+]?((([0-9]{1,3})([,][0-9]{3})*)|([0-9]+))?([\.]([0-9]+))?$/,
                    "alertText": "Invalid floating decimal number"
                },

                //tls warning:homegrown not fielded                 
                "images": {
                    "regex": /^[\w,\s-]+\.[jpg|png|gif]{3}$/,
                    "alertText": "Upload .jpg or .png or .gif format"
                },
                //tls warning:homegrown not fielded                 
                "pdf": {
                    "regex": /^[\w,\s-]+\.[pdf]{3}$/,
                    "alertText": "Upload .pdf format only"
                }                               
            };

        }
    };

    $.validationEngineLanguage.newLang();

})(jQuery);

这是我的验证脚本但是 IE中的图像失败,其他浏览器没问题

            "images": {
                "regex": /^[\w,\s-]+\.[jpg|png|gif]{3}$/,
                "alertText": "Upload .jpg or .png or .gif format"
            },

2 个答案:

答案 0 :(得分:1)

当你写[pdf]{3}时,你匹配['p', 'd', 'f']中的任何一个角色,重复三次。因此,以下扩展名将有效:

dfp
dpf
fdp
fpd
pdf
pfd

这不是你想要的。

[pdf]{3}替换为pdf

同样,[jpg|png|gif]{3}应替换为(jpg|png|gif)

答案 1 :(得分:0)

IE获取文件的完整路径,这就是它失败的原因,所以只需编辑正则表达式如下

/ ^ [\ W,\ S - / \:] +(JPG | PNG | GIF)$ /