0

I am new to AngularJS and I just started to tweak around the validations that come with it but I can't get them to work for some reason:

<div class="row">
    <div class="col-xs-6">
        <h2>Login</h2>
        <p class="text-danger">{{message}}</p>
        <form name="form" novalidate>
            <label>Email: </label>
            <input type="email" ng-model="credentials.email" placeholder="Email" class="form-control" required />
            <label>Password: </label>
            <input type="password" ng-model="credentials.password" placeholder="Password" class="form-control" />
            <br />
            <button class="btn btn-primary" ng-click="authenticate(credentials)">Login</button>
            <a href="#/register">Register</a>
        </form>
    </div>
</div>

I named the form and put the novalidate also. Then I tried to have the email input as required but putting the attribute there. But the form still submits and AngularJS does not stop it from submitting or shows any warnings. What am I missing?

1
  • Enable/disable button based on form validity. Commented Jan 15, 2015 at 7:53

2 Answers 2

1

Provide name field to your input

 <form name="form" novalidate>
            <label>Email: </label>
            <input type="email" name="email" ng-model="credentials.email" placeholder="Email" class="form-control" required />
            <label>Password: </label>
            <input type="password" ng-model="credentials.password" placeholder="Password" class="form-control" />
            <br />
            <button class="btn btn-primary" ng-disabled="form.$invalid" ng-click="authenticate(credentials)">Login</button>
            <a href="#/register">Register</a>
        </form>

And here is the plunker :-) http://plnkr.co/edit/pAA2wBp8zgIZV6QLwO7S?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

So Angular does not show warnings automatically? It just pushes the classes ng-invalid, ng-pristine, ng-dirty and so on. I thought it worked similar to bootstrap?
Yes exactly you have to validate it by yourself ,it just throw errors:-)
0

Remove required attribute from email input if you want to make optional.

<input type="email" ng-model="credentials.email" placeholder="Email" class="form-control"/>

If you want to remove email validation change the field type.

HTML5 always validate email if you make type="email"

<input type="text" ng-model="credentials.email" placeholder="Email" class="form-control"/>

2 Comments

Still not working. That did not even make sense? I have tried inserting required at password also. Still nothing.
Updated answer. Please check

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.