【问题标题】:Can not validate password field with reg exp using angular.js无法使用 angular.js 使用 reg exp 验证密码字段
【发布时间】:2015-10-28 05:03:12
【问题描述】:

我需要使用一些特殊字符来验证我的密码字段。该字段应包含至少一个数字、大写字母、小写字母和下划线,这些都是强制性的。我正在使用 ng-patternng-message 但未能做到这个。我在下面解释我的代码。

<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Password :</span>
<div ng-class="{ 'has-error': billdata.pass.$touched && billdata.pass.$invalid }">
<input type="{{inputType}}" name="pass" id="contactno" class="form-control" placeholder="password" ng-model="password" ng-minlength="8" ng-pattern="/[a-zA-Z][0-9][a-zA-Z0-9]{3}/." >
</div>
</div>
<div class="help-block" ng-messages="billdata.pass.$error" ng-if="billdata.pass.$touched">
<p ng-message="minlength" style="color:#F00;">This field is too short.The min length of your password should be 8.</p>
<p ng-message="pattern" style="color:#F00;">This field needs the special character like number,upper case,lower case letter,underscore.</p>
</div> 

这里我没有收到任何错误消息。请帮助我。

【问题讨论】:

  • 从正则表达式中删除最后一个.
  • @Tushar : 需要下划线吗?
  • 不,试试ng-pattern="/[a-zA-Z]\d\w{3}/",但这将允许字符顺序相同
  • @Tushar :我使用了你的模式。但是在输入所有类型字符之后仍然会出现错误。

标签: javascript regex angularjs validation


【解决方案1】:

字段应至少包含一个数字、大写字母、小写字母和下划线,这些都是强制性的。

您需要使用基于前瞻的正则表达式。

ng-pattern="/^(?=.*[A-Z])(?=.*\d)(?=.*[a-z]).*_.*/"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2016-01-05
    • 2015-05-12
    • 2018-03-01
    相关资源
    最近更新 更多