1

Hi I am developing angularjs application. I have textbox and i have custom directive attached to it. It should accept certain range of numbers for example between 100 to 200. It is working fine. Along with these validation i want required field validator on submitting form. At a time only one validation should be displayed. Below is my textbox.

<div class="inputblock" ng-class="{ 'has-error' : ((form5.$submitted && form5.rangeNumber.$invalid )|| (form5.rangeNumber.$invalid && form5.rangeNumber.$dirty))}">
    <label class="inputblock-label">{{'Down Payment' | translate}}</label>
    <div>
        <span class="ang-error" style="color:#fff" ng-show="form5.rangeNumber.$dirty && form5.rangeNumber.$invalid">
                                     <span ng-show="!(form5.$submitted && form5.rangeNumber.$error.required)&&form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">
                                         {{ 'Value should be between' | translate }} {{min}} {{'and' | translate}} {{max}}
                                     </span>
        </span>
    </div>
    <input class="" type="text" name="rangeNumber" ng-attr-placeholder="{{ 'DownPayment' }}" ng-model="DownPayment" range-number="range" required>
</div>

validation error May i know if this can be done?

6
  • You can use ng-minlength and ng-maxlength to validate range instead of custom directive Commented Jul 19, 2017 at 13:36
  • Thank you. I already have directive and it is working as expected. Commented Jul 19, 2017 at 13:37
  • What errors are showing together? Commented Jul 19, 2017 at 13:39
  • i am able to display only directive error. I have attached error. whenever i select on submit form i want to display required and when the user clicks on textbox then the range message should be displayed. Commented Jul 19, 2017 at 13:41
  • So you want to display required error if the above field is empty on submitting of form? Commented Jul 19, 2017 at 13:51

1 Answer 1

1

The below is for directives error.

   <span ng-show="form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">
        {{ 'Value should be between' | translate }} {{min}} {{'and' | translate}} {{max}}</span>
       </span>

The required error on submitting

<span ng-show="form5.$submitted && form5.rangeNumber.$error.required">
          Required*
</span>

Combination to avoid both coming together

   <span ng-show="!(form5.$submitted && form5.rangeNumber.$error.required)&&form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">
        {{ 'Value should be between' | translate }} {{min}} {{'and' | translate}} {{max}}</span>
       </span>
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you.I left office now. Tom I will test
Okay. let me know if there is any problem
Hi Vivz. Hope you are doing good. I applied validation but it is overlapping. I have attached screen shot.
Its working but whenever i enter some invalid number then if i click on submit and come back to textbox and if i clear i get red colour without any text. i have updated my code and picture.
It is because your ng-class is evaluating to true
|

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.