当前位置: 动力学知识库 > 问答 > 编程问答 >

validation - Comparing two fields and form.$valid

问题描述:

What i want to realize is two input fields which i want to compare. If same, form has to be valid. Otherwise, invalid. This is the code:

<form name="form" class="css-form" novalidate>

E-mail: <input type="email" placeholder="[email protected]"

ng-model="user.email" name="uEmail" required/>

<span class="error" ng-show="form.uEmail.$error.email">

Not valid email!</span>

Repeat e-mail: <input type="email" placeholder="[email protected]"

ng-model="repEmail" required/>

<span ng-if="user.email != repEmail">

E-mail address are not same!

</span>

</form>

The problem is, although these fields are not same, it is true:

form.$valid == true

Thus, how can i change the validity of form so that it can be false if the input fields are not same (although they are valid e-mail addresses)

网友答案:

You can use two methods here. You can either use angular-ui's validator directive (which is probably the easiest way to go), or you can write you own directive.

If you decide to use angular-ui's validator, it would look something like this:

<input name="email" required ng-model="user.email">
<input name="confirm_email"
    ui-validate=" '$value==email' "
    ui-validate-watch=" 'email' ">
<span ng-show="form.confirm_emal.$error.validator">Emails do not match!</span>
网友答案:

just change the type to email

Repeat e-mail: <input type="email" placeholder="[email protected]" 
            ng-model="repEmail"  required/>

edit:

instead of just using
form.$valid
use
form.$valid && user.email == repEmail

分享给朋友:
您可能感兴趣的文章:
随机阅读: