0

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)

1

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate this being here, but I'm interested in writing a custom validator! I've made that a separate, specific question, would you be able to answer it? stackoverflow.com/questions/20982751/…
0

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

2 Comments

it does not matter. What matter is just that they have to be equal. as you say, it might also be e-mail but the problem is not that
for you to submit the page, there are 2 things<br/> 1.) form should be valid. which can be achieved from form.$valid, after setting repEmail type to be email<br/> 2.) email and repEmail are equal<br/> so instead of just using <br/> if(form.$valid) submit();<br/> use if(form.$valid && $scope.user.email != $scope.repEmail) submit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.