1

<md-input-container flex>
  <label>{{'COMMON.AMOUNT' | translate }}</label>
  <input class="margin-top-2" id="depositpaidamount" enterastab name="disabledamount" type="number" ng-model="deposit.paidamount" ng-disabled="true" />
</md-input-container>

This field allows to enter decimal also. But i want to enter only integer.

3 Answers 3

1

You need to add a check in JavaScript Controller for this if entered value is a integer or not.

Below is working code:

var invalidChars = ["-","+","e","."];

depositpaidamount.addEventListener("keydown", function(e) {
  if (invalidChars.includes(e.key)) {
    e.preventDefault();
  }
});
<md-input-container flex>
  <label>{{'COMMON.AMOUNT' | translate }}</label>
  <input class="margin-top-2" id="depositpaidamount" enterastab name="disabledamount" type="number" ng-model="deposit.paidamount" ng-disabled="true" />
</md-input-container>

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

2 Comments

Is there any option to do only in html
@SrividhyaS you need to take care of for case of copy-paste as well. This code will prevent user to enter non-integer from keyboard as well as from copy-paste. Tester will raise a bug for this, so Keep in mind :)
0

use onkeypress event in your input box

<input type="text" onkeypress='return (event.charCode >= 48 && event.charCode <= 57) || event.charCode <= 31'>

7 Comments

This is not correct answer. User can enter dot which should not be.
i edited my answer jst removing 46 key code, try again
But this will not work if user do copy-paste. User can not enter non-integer from keyboard BUT can paste
use - ** onpaste="return false" **
onpaste="return false" is not good option. What if user want to paste a valid integer?
|
0

Use Directive

app.directive('restrictTo', function () {
return {
    restrict: 'A',
    link: function (scope, element, attrs) {
        var re = RegExp(attrs.restrictTo);
        var exclude = /Backspace|Enter|Tab|Delete|Del|ArrowUp|Up|ArrowDown|Down|ArrowLeft|Left|ArrowRight|Right/;

        element[0].addEventListener('keydown', function (event) {
            if (!exclude.test(event.key) && !re.test(event.key)) {
                event.preventDefault();
            }
        });
    }
}
});

And In HTML we use following way:
<input type="text" restrict-to="[0-9]"/>

See the Plunker Example HERE.. Visit: https://plnkr.co/edit/PVjVgKOqZWojDJUYxKt4?p=preview

If useful to you please mark it as answer.

1 Comment

@ShrividyaS - my example above will take care of copy-paste issue as well.

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.