ng-pattern无法正常工作,匹配每个字符串 [英] ng-pattern not working properly, matching every string

查看:199
本文介绍了ng-pattern无法正常工作,匹配每个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用模式匹配来验证密码字段.我为此使用了ng-pattern.但是它不能正常工作.

I need to validate a password field with pattern matching. I used ng-pattern for that. But it is not working properly.

请参见此插件链接

我在这里给出的模式是,文本至少应包含8个字符,包括符号,字母和数字.但是,当我输入任何字符串时,模式匹配将变为true.

The pattern I gave here is, the text should contain at least 8 characters with symbols, alphabets and digits. But when I enter any string the pattern match become true.

请参见以下代码

<body ng-controller="MainCtrl">
    <form name="form">
      <input type="text" name="password" ng-model="user.password" ng-pattern="/(^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[\W]).*$)/">
      <input type="submit">
      {{form.password.$error}}
    </form>
</body>

我在这里想念什么?

推荐答案

这似乎有效.如果字段无效,则表达式 {{form.password.$ error}} 将变为 true .空字段不会得到验证.

This seems to be working. The expression {{form.password.$error}} becomes true if the field is invalid. The empty field does not get validated.

因此,输入 aaaaa (5个字母)会使该字段无效.由于长度的原因,添加 1 @ (数字和符号)仍然无效.添加任何内容即可使其生效(> = 8个字符,包括字母,数字,符号&rarr;显示屏显示 false ).

So entering aaaaa (5 letters) makes the field invalid. Adding 1@ (digit+symbol) is still invalid due to length. Adding anything makes it valid (>= 8 characters with letters, digits, symbols → the display says false).

这篇关于ng-pattern无法正常工作,匹配每个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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