1

I am beginner of angularjs. I just started on form validation. All is went right until i tried to hide the error message on input focus.

When i am hitting submit button it shows error message. After i focus on textbox it has to hide the error message

And this is my code:

  <form name="form" novalidate>
        <div>
            <label>Username</label>
            <input type="text" name="username" ng-model="username" ng-required="true" ng-minlength="5" ng-maxlength="7"/>

            <span class="errors" ng-show="(form.$submitted || form.username.$touched) && form.username.$error.required">Please fill username</span>
            <span class="errors" ng-show="form.username.$error.minlength">Username too short</span>
            <span class="errors" ng-show="form.username.$error.maxlength">Username too long</span>
        </div>
  </form>

Please someone help me

4
  • can you describe your problem a bit more? what did you do to "hide" the error message? Commented Jul 16, 2015 at 11:58
  • Now i updated the question Commented Jul 16, 2015 at 12:02
  • Can you create a fiddle for with the issue? Commented Jul 16, 2015 at 12:04
  • Consider using ng-focus docs.angularjs.org/api/ng/directive/ngFocus Commented Jul 16, 2015 at 12:12

3 Answers 3

3

this is just a trick, try if this works :

<form name="form" novalidate>
    <div>
        <label>Username</label>
        <input type="text" name="username" ng-model="username" ng-required="true" ng-minlength="5" ng-maxlength="7" ng-focus="focused=true"/>

        <span class="errors" ng-show="(form.$submitted || form.username.$touched) && form.username.$error.required && !focused">Please fill username</span>
        <span class="errors" ng-show="form.username.$error.minlength && !focused">Username too short</span>
        <span class="errors" ng-show="form.username.$error.maxlength && !focused">Username too long</span>
    </div>
</form>

NB : on clicking submit, make focused = false;

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

1 Comment

did you try focused = false on submiting ?
0

I guess you forgot to mention ng-app directive

Comments

0

use ng blur for show instant message on just leaving input field :

<form name="form" novalidate>
    <div>
        <label>Username</label>
        <input type="text" name="username" ng-model="username" ng-required ng-minlength="5" ng-maxlength="7" ng-blur="focused=false" ng-focus="focused=true"/>

        <span class="errors" ng-show="(form.$submitted || form.username.$touched) && form.username.$error.required && !focused">Please fill username</span>
        <span class="errors" ng-show="form.username.$error.minlength && !focused">Username too short</span>
        <span class="errors" ng-show="form.username.$error.maxlength && !focused">Username too long</span>
    </div>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.