I am doing some validation on text boxes. Once the user enters a valid input, the text box should bee green and a tick mark should appear at the rightmost corner. Similarly, if the user has entered an invalid data, text box should bee 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
I am doing some validation on text boxes. Once the user enters a valid input, the text box should bee green and a tick mark should appear at the rightmost corner. Similarly, if the user has entered an invalid data, text box should bee 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
Share Improve this question edited Mar 5, 2014 at 11:56 Ren asked Mar 4, 2014 at 18:08 RenRen 1,5036 gold badges32 silver badges56 bronze badges 4- 3 Did you try (Form.emailAddress.$valid && Form.emailAddress.$dirty) so the field is only green if it is dirty and valid? – Craig Squire Commented Mar 4, 2014 at 18:52
- Yes. I tried ng-class="(contactDetailsForm.emailAddress.$valid) && contactDetailsForm.emailAddress.$dirty ? 'has-success has-feedback': 'has-error has-feedback'" but then the textbox bees red by default when the page is loaded. – Ren Commented Mar 5, 2014 at 9:25
- You need two separate checks. $valid && $dirty to check for green and a separate $invalid && $dirty to check for red. – Craig Squire Commented Mar 5, 2014 at 12:14
- So, Should I use to ternary operations with in ng-class ? Could you please show it with an example ? – Ren Commented Mar 5, 2014 at 12:37
1 Answer
Reset to default 5You 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>