HTML 5输入正则表达式模式不起作用 [英] HTML 5 Input regex pattern not working

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

问题描述

由于某种原因,我无法在HTML5表单验证模式属性中使用此功能.我正在尝试匹配此条件/[!?^$\\\/{}]/g.我尝试使用全局属性转义\删除\,删除[],几乎所有内容.因此,始终是第一次在stackoverflow上发布问题.

For some reason I'm having problems making this work with HTML5 form validation pattern attribute. I'm trying to match this condition /[!?^$\\\/{}]/g. I have tried escaping \ removing the \ with the global attribute, removing the [], almost everything. So there's always a first time to post a question on stackoverflow.

简而言之,用户不应输入以下\/${}

In a nutshell the user shouldn't input the following \/${}

<form action="#">
    <input type="text" required pattern="/[!?^$\\\/{}]/g">
    <button type="submit">Submit</button>
</form>

4.20 - 4.21 New export->应该有效

4/20 - 2/21 $New {export}->应该无效

这是小提琴: http://jsfiddle.net/TV28A/

推荐答案

我已经尝试了上述所有答案以及所有有效的(不知道我测试的地方是否错误).但以防万一:

I have tried all the answers above and any worked (don't know if I am testing wrong). But Just in case:

我认为,如果您只想阻止\/${}字符,则应该是正确的正则表达式[^\/\\${}]*

I think if you want to block only the \/${} characters, this should be the right regex [^\/\\${}]*

因此代码将类似于:

<form action="#">
    <input type="text" required pattern="[^\/\\${}]*">
    <button type="submit">Submit</button>
</form>

^字符作为第一个字符放在方括号中,它使句子取反,因此[^\/\\${}]*表示具有任意大小的/ \ $ { }的字符串.

Putting the ^ character as the first one into the brackets, it negate the sentence, so [^\/\\${}]* means string which do NOT have / \ $ { } of any size.

按照评论中的要求共享 jsFiddle .

Sharing the jsFiddle as requested in the comments.

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

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