私のアプリのユーザー登録ページに検証を追加していますが、ngMessages
と組み込みのanglejs検証を使用しようとしています。angularjs - ngMessageと検証が正常に動作しない
パスワードマッチングや一般的なパターンで奇妙な動作が発生しています。
<span>Password (6 to 16 character)</span>
<span class="item item-input">
<input type="password"
ng-model="userRegistration.password"
placeholder="password"
name="password"
ng-minlength="6"
ng-maxlength="16"
required
ng-class="{'invalid-input': userRegistrationForm.password.$touched && userRegistrationForm.password.$invalid }">
</span>
<!-- Form validation messages -->
<div role="alert" class="error-message" ng-messages="userRegistrationForm.password.$error" ng-show="userRegistrationForm.password.$touched">
<p ng-message="required">This field is required</p>
<p ng-message="minlength">This field is too short</p>
<p ng-message="maxlength">This field is too long</p>
</div>
<span>Confirm Password</span>
<span class="item item-input">
<input type="password"
placeholder="confirm password"
ng-model="userRegistration.passwordConfirmation"
name="confpass"
ng-minlength="6"
ng-maxlength="16"
ng-pattern="/^{{userRegistration.password}}$/"
required
ng-class="{'invalid-input': userRegistrationForm.confpass.$touched && userRegistrationForm.confpass.$invalid }">
</span>
<!-- Form validation messages -->
<div role="alert" class="error-message" ng-messages="userRegistrationForm.confpass.$error" ng-show="userRegistrationForm.confpass.$touched">
<p ng-message="required">This field is required</p>
<p ng-message="pattern">Password not matching!</p>
</div>
問題は、パスワードが一致しても、エラーがそれが一致していないことをdisplayng、私はなぜそれがこれをやっていることは理解できないままということです。
は、ここでパスワードの照合のためのコードです。
期待通りに動作しないパターンの別の例は、以下のコードに見出すことができる:
<span>System ID</span>
<span class="item item-input">
<input type="text"
ng-model="userRegistration.id"
placeholder="system id"
name="systemID"
ng-minlength="6"
ng-maxlength="6"
pattern="/:/"
required
ng-class="{'invalid-input': userRegistrationForm.systemID.$touched && userRegistrationForm.systemID.$invalid }">
</span>
<!-- Form validation messages -->
<div role="alert" class="error-message" ng-messages="userRegistrationForm.systemID.$error" ng-show="userRegistrationForm.systemID.$touched">
<p ng-message="required">This field is required</p>
<p ng-message="pattern">Colons not required!</p>
<p ng-message="minlength">This field is too short</p>
<p ng-message="maxlength">This field is too long</p>
</div>
私は、彼らがいる場合にコロンを入力し、表示し、エラーに追加されるかどうかを確認する必要があります。前述のように、たとえコロンがないとしても、メッセージは表示されます。
私には何が欠けていますか?私は明らかにng-pattern
とng-messages
を間違って使用していますが、なぜそれを理解できません。
本当にありがとうございます。
ここで両方の例でCodepen:http://codepen.io/NickHG/pen/NNZYwK?editors=1010
おかげ
上記の例をありがとう、私はそれを念頭に置いておきます。しかし、なぜメソッドのデフォルトのビルドが正しく動作していないのか考えていますか? – Nick
あなたのような解決策で自分の答えを更新しました。違いは単にコントローラ内の正規表現を作り、それを 'ng-pattern'に束縛することだけです。また、私の正規表現は少し異なります。 – thepio
更新していただきありがとうございますが、パスワードとして「a」と入力し、確認として「a」を入力した場合、メッセージは以前のように表示されます。 – Nick