HTML输入模式不起作用 [英] HTML input pattern not working

查看:164
本文介绍了HTML输入模式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*(\d {3} \-\d {3} \-\d {2} \-\d {2} |。\d { 3} \ -\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\精细。但突然它最近停止在镀铬和歌剧中工作。这里发生了什么 ?这里有什么问题,这是怎么回事? Opera正在通知无效转义,在chrome中也是如此。它在js中检查时工作正常。

< form>< ; input type =textpattern =。*(\d {3} \ -\\\ {3} \\\\\\\\\\\\\\\\\\\\\\ {3} \-\d {2} \-\d {2} \-\d {3} | \d {10})*>。<按钮>置于< /按钮>< / form>

解决方案

关键在于Chrome和Firefox已经支持ES6正则表达式规范,并默认支持Unicode模式。



Unicode模式对于哪些字符可以是更严格的规则在模式内逃脱。请参阅此参考资料


IdentityEscape :在BMP模式中,许多字符可以以反斜杠作为前缀并被解释为它们自己(例如:if \u 后面跟着四个十六进制数字,它被解释为 u )。 在只适用于以下字符的Unicode模式中(Unicode编码点转义可以释放 \u ): ^ $ \ * + [ ] { } |

/ blockquote>

同一组字符在 SyntaxCharacter ecma-262 / 6.0 /#sec-patternsrel =nofollow> ES6规格页

所以,你只能逃避 - 在它被认为是特殊字符的字符类中,并且使它成为一个字面值,您可以将其转义。在其他地方它不能被转义。



< form> < input type =textpattern =。*(\d {3} -\\d {3} -\\d {2} -\ {2} | \d {3} - \\d {2} -\d {2} -\d {3} | \d {10})*>。 <输入类型=提交> < / form>


.*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).* this pattern was working fine. But suddenly it stop working in chrome and opera lately. What's going on here ? What a problem is here and how it's wrong? Opera is informing about invalid escape, same in chrome. It works fine when im checking it in js.

<form>
<input type="text" pattern=".*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).*">
<button>
Send
</button>
</form>

解决方案

The point is that Chrome and Firefox already support ES6 regex specifications and support the Unicode mode by default.

Unicode patterns have stricter rules as to what characters can be escaped inside the pattern. See this reference:

IdentityEscape: In BMP patterns, many characters can be prefixed with a backslash and are interpreted as themselves (for example: if \u is not followed by four hexadecimal digits, it is interpreted as u). In Unicode patterns that only works for the following characters (which frees up \u for Unicode code point escapes): ^ $ \ . * + ? ( ) [ ] { } |

The same set of chars is referred to as SyntaxCharacter in the ES6 specs page.

So, you can only escape the - inside the character class where it is considered a special character and to make it a literal you can escape it. Everywhere else it must not be escaped.

<form>
  <input type="text" pattern=".*(\d{3}-\d{3}-\d{2}-\d{2}|\d{3}-\d{2}-\d{2}-\d{3}|\d{10}).*">
  <input type=Submit>
 </form>

这篇关于HTML输入模式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆