0

I want to set Max-length for input element using angularJS but its not working for me. Below is what I tried.

 <div class="billcutofdate vLine vLineRight" id="SAPExecutiveBillDate">
            <a><i class="fa fa-calendar-check-o" aria-hidden="true"></i> Billing Cut-off Date <span class="badge">{{BillCutdate}}</span></a>
            <div class="setcutofdate">
                <div class="inputNumber clearfix"><input ng-maxlength="2" type="number" step="1" value="0" class="form-control" ng-model="BillCutdateSet" /></div>
                <button class="btn btn-default customBtn" ng-click="changeBillDate()"><i class="fa fa-calendar-check-o" aria-hidden="true"></i> Set Date</button>
            </div>
        </div>

Can anyone tell me why its not working as I am new to angularJS

UPDATE

REPRODUCING ERROR

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.5/angular.min.js"></script>








<input ng-minlength="1" ng-maxlength="2" min="1" max="31" type="number" step="1" value="0" class="form-control" ng-model="BillCutdateSet" />

10
  • What does it mean that is not working? If you try to validate the form the angularjs way does it says is valid? If you want the browser no to let the user to type more than something, use maxlength attribute + ng-maxlength for angularjs validation Commented Apr 19, 2019 at 10:22
  • @f-CJ: Not working means I am able to add characters digit more than 2 numbers. See my updated question that I tried Commented Apr 19, 2019 at 10:24
  • ng-maxlength is not going to prevent that, html attribute maxlength does that Commented Apr 19, 2019 at 10:25
  • ok, but how can we do that using angularJS Commented Apr 19, 2019 at 10:26
  • even html attribute is not working Commented Apr 19, 2019 at 10:27

1 Answer 1

0

That is a simple functionality but sometime browser gets confused if the input type=number.

So by taking some reference from Here see Raghavendra's answer. I tried this and achieved it.

<input ng-minlength="1" ng-maxlength="2" min="1" max="31" onkeypress="return (event.charCode !=8 && event.charCode ==0 || (event.charCode >= 48 && event.charCode <= 57))" readonly="readonly" type="number" step="1" value="0" class="form-control" ng-model="BillCutdateSet" />
Sign up to request clarification or add additional context in comments.

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.