AngularJS + Bootstrap - Validating Text Box - how to show validation feedback only after the user has entered some data?

StackOverflow https://stackoverflow.com/questions/22179644

Question

I am doing some validation on text boxes. Once the user enters a valid input, the text box should become green and a tick mark should appear at the rightmost corner. Similarly, if the user has entered an invalid data, text box should become red and a cross should appear instead.

For this, I'm using bootstrap's 'has-error has-feedback' and 'has-success has-feedback' css classes.

My problem is that the textbox is green and has a tick even when the page is loaded for the first time. I need the validation feedback to appear only after the user has entered an input. And the same applies when the form is used to edit the existing records. How can I achieve this ? (I'm not sure if I could use $dirty and '$pristine` to solve this problem)

Below is my Markup.

<div class="form-group" ng-class="(Form.emailAddress.$valid) ? 'has-success has-feedback': 'has-error has-feedback'">
    <label for="emailAddress">Email address</label>
    <span ng-show="!Form.emailAddress.$valid">Please enter a valid address</span>

    <input type="email" class="form-control" id="emailAddress" name="emailAddress" ng-model="Data.emailAddress" ng-pattern="validationPattern" ng-maxlength="20">

    <span ng-show="Form.emailAddress.$valid" class="glyphicon glyphicon-ok form-control-feedback"></span>
    <span ng-show="!Form.emailAddress.$valid" class="glyphicon glyphicon-remove  form-control-feedback"></span>
</div>

A few more questions regarding this topic.

I would like to check the maxlength of the email as well, and if it exceeds 20, I would want to show a different message. I tried Form.emailAddress.$maxlength but couldn't get it working. Does it maxlength work on 'email' types as well ?

How can I show the feedback after the user moves to the next textbox instead of showing it while typing ? May be something similar to jquery's 'focusout()'.

I've used a validation pattern in the controller because email is not a required field, but when entered it has to be valid. Could I please know if there are better ways than doing this ?

Any help would be much appreciated. Thanks

Was it helpful?

Solution

You have to use $valid && $dirty for green and in a separate check, use $invalid and $dirty for red.

Here's an example: http://plnkr.co/edit/k8X5NSeUrBb2nqA9yD9F

<form name="myform">
  Valid: {{myform.myinput.$valid}}<br/>
  Dirty: {{myform.myinput.$dirty}}<br/>
  <input type="text" 
         name="myinput" 
         required 
         ng-model="myvalue" 
         ng-class="{'has-success has-feedback': \
                     myform.myinput.$valid && myform.myinput.$dirty, \
                    'has-error has-feedback': \
                     myform.myinput.$invalid && myform.myinput.$dirty}"></input>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top