AngularJS - 无效的正则表达式

时间:2017-10-09 07:26:17

标签: javascript angularjs regex

我使用此RegEx验证输入的密码。密码需要至少有1个大写字母,1个数字和1个符号:

/(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z\d])/

我在ng-pattern输入中使用RegEx和type="password"。输入Mike123@Mike123*后,它可以正常工作。但是在输入Mike123***时会抛出以下错误:

SyntaxError: Invalid regular expression: /^Mik123***$/: Nothing to repeat

RegEx在这里工作正常:https://regex101.com/r/j0O4aw/1。正在使用的代码:

<div class="input_fieldholder">
  <input type="password"
  class="m-b-0 login_typefield"
  placeholder="Password"
  name="password"
  ng-class="{ form_error: vm.formObject.password.$dirty && vm.formObject.password.$invalid }"
  ng-model="vm.password"
  ng-minlength="6"
  ng-change="vm.onChangePassword()"
  ng-pattern="/(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z\d])/"
  required
  ng-attr-autofocus="{{ vm.autoFocus }}">

  <div class="text-left m-t-3">
    <div ng-show="vm.formObject.password.$dirty && vm.formObject.password.$error.required" class="text-danger">{{ 'PASSWORD_REQUIRED_VALIDATION' | translate }}</div>
    <div ng-show="vm.formObject.password.$dirty && vm.formObject.password.$error.minlength" class="text-danger">{{ 'PASSWORD_LENGTH_VALIDATION' | translate }}</div>
    <div ng-show="vm.formObject.password.$dirty && vm.formObject.password.$error.pattern" class="text-danger">{{ 'PASSWORD_PATTERN_VALIDATION' | translate }}</div>
  </div>
</div>

<div class="input_fieldholder">
  <input type="password"
  class="m-t-10 m-b-0 login_typefield"
  placeholder="Confirm Password"
  name="confirmPassword"
  ng-class="{ form_error: vm.formObject.confirmPassword.$dirty && vm.formObject.confirmPassword.$invalid }"
  ng-model="vm.confirmPassword"
  ng-pattern="vm.password"
  ng-change="vm.onChangePassword()"
  required
  compare-field-selector="[name='password']">
  <div class="text-left m-t-3">
    <span ng-show="vm.formObject.confirmPassword.$dirty && vm.formObject.confirmPassword.$invalid" class="text-danger">{{'PASSWORD_CONFIRM_VALIDATION' | translate }}</span>
  </div>
</div>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

来自示例。

body { background-color: lightgray; } ul { position: absolute; top: 50px; left: 50px; list-style: none; } .element { position: absolute; top: 0; left: 0; background-color: red; }”包含要匹配的正则表达式。 “<ul> <li> <!--Other Content --> </li> </ul> <div class="element"> <p> Element </p> </div>”包含要检查正则表达式的字符串。

id=regex

接下来是:

id=input