输入模式在 IE11 中不完全匹配,但在 Chrome 中,FF

Input pattern doesn't match completely in IE11, but in Chrome, FF

以下输入字段在 Chrome 和 FF 中完全有效,但在 IE11 中仅部分有效。

<input name="bic" value="" type="text" title="BIC-Code" required
 pattern="[A-Z]{6}[A-Z2-9][A-NP-Z0-2](|[A-WY-Z0-9][A-Z0-9]{2}|X{3})"
>

使用 8 个字符的 BIC 测试代码始终适用于任何浏览器。用 11 个字符测试它会在 IE 中产生错误。

当前安装版本:11.0.9600.17959,更新版本:11.0.22

示例:

问题:

Google 唯一找到的是 type='number' "problem" 和 ^[0-9]*$

http://www.w3.org/TR/2011/WD-html5-20110525/common-input-element-attributes.html#the-pattern-attribute 说:

This implies that the regular expression language used for this attribute is the same as that used in JavaScript, except that the pattern attribute must match the entire value, not just any subset (somewhat as if it implied a ^(?: at the start of the pattern and a )$ at the end).

甚至 Javascript-Engine 在 IE11 中也能正确验证:

new RegExp("^[A-Z]{6}[A-Z2-9][A-NP-Z0-2](|[A-WY-Z0-9][A-Z0-9]{2}|X{3})$").test("GENODEF1S04");

IE11 似乎无法处理第一个分支为空的交替。这个:

(|[A-WY-Z0-9][A-Z0-9]{2}|X{3})

...应该等同于:

((?:[A-WY-Z0-9][A-Z0-9]{2}|X{3})??)

...意思是 [A-WY-Z0-9][A-Z0-9]{2}X{3} 或什么都不是,"nothing" 是第一个(或首选)选项。

我建议不要使用 "or nothing" 习语,即使末尾有空分支(这在 IE11 中似乎工作正常)。这不是很常见,我认为可选组可以更好地传达您的意图。