Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
XML
<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>

I also tried
HTML
<div ng-class="(contactDetailsForm.emailAddress.$valid) && contactDetailsForm.emailAddress.$dirty ? 'has-success has-feedback': 'has-error has-feedback'">

but then the textbox becomes red by default when the page is loaded.

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
Posted
v2

1 solution

Use ngHide[^] and ngShow[^].
Show the green tick only when the user has entered data that is valid.
 
Share this answer
 
Comments
devdev13 5-Mar-14 7:46am    
I've already used 'ng-show' within the span tags to show the validation feedback based on the input. Should I use it along with ng-class in the div as well ? Could you please provide an example ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900