Shruggie
Shruggie

Reputation: 938

ng-minlength Angular Form Validation

Angular seems to not be raising minLength or maxLength error in the below code... the required error (as well as the email error) is working however. I know ng-minlength and ng-maxlength is working because the input box is changing its CSS. The text inside the <span> is not working for the min or max errors.

See the password input below:

<section class="row" data-ng-controller="AuthenticationController">
<h3 class="col-md-12 text-center">Sign Up</h3>
<div class="col-xs-offset-2 col-xs-8 col-md-offset-5 col-md-2">
    <form name="userForm" data-ng-submit="userForm.$valid &amp;&amp; signup()" class="signin form-validate" novalidate autocomplete="off">
        <fieldset>
            <div class="form-group">
                <span ng-show="userForm.email.$dirty &amp;&amp; userForm.email.$error.required" class="text-danger">This field is required</span>
                <span ng-show="userForm.email.$dirty &amp;&amp; userForm.email.$error.email" class="text-danger">This field must be a valid email address</span>
            </div>
            <div class="form-group">
                <label for="username" class="control-label">Username</label>
                <input type="text" id="username" name="username" class="form-control" data-ng-model="credentials.username" placeholder="Username">
            </div>
            <div class="form-group">
                <label for="password" class="control-label">Password</label>
                <input type="password" id="password" name="password" class="form-control" required data-ng-model="credentials.password" ng-minlength="8" ng-maxlength="24" placeholder="Password">
                <span ng-show="userForm.password.$dirty &amp;&amp; userForm.password.$error.required" class="text-danger">This field is required</span>
                <span ng-show="userForm.password.$dirty &amp;&amp; userForm.password.$error.minLength" class="text-danger">Please use a password of at least 8 characters</span>
                <span ng-show="userForm.password.$dirty &amp;&amp; userForm.password.$error.maxLength" class="text-danger">The charactar limit for passwords is 24</span>
            </div>
            <div class="text-center form-group mt">
                <button type="submit" class="btn btn-large btn-primary">Sign up</button>&nbsp; or&nbsp;
                <a href="/#!/page/signin" class="show-signup">Sign in</a>
            </div>
            <div data-ng-show="error" class="text-center text-danger">
                <strong data-ng-bind="error"></strong>
            </div>
        </fieldset>
    </form>
</div>

any thoughts on what's gone wrong here?

Upvotes: 2

Views: 975

Answers (2)

Shruggie
Shruggie

Reputation: 938

It was just a syntax error: userForm.password.$error.minLength should have been userForm.password.$error.minlength (capitalization).

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

You should change &amp;&amp; to && inside ng-show expression, you must have getting an error in console.

The updated ng-show version will look something like below.

ng-show="userForm.password.$dirty && userForm.password.$error.required"

The other more convenient way is to use ng-messages directive, to show and hide validation messages based on form & its field validity

Upvotes: 0

Related Questions