Angular 2 - accept only natural number in input

TheUnreal

I have a shop component and I need to disallow people to pick negative numbers in my input.

For example, if user will type -3 in my input, the input value will immadiately change to 0.

How I can control my angular 2 input OnChange value to set it back to 0 it the value is negative?

Tried to use (ngModelChange)="myMethod($event)" and set the parameter to 0 if it's negative, but didn't work.

Mark Rajcok
  • Use min and step to prevent the spinner from going negative. (reference: this SO answer)
  • Use ngModelChange to handle the case where a user tries to type in a negative number.
@Component({
  selector: 'my-app',
  template: `{{title}}
    <p><input type="number" min="0" step="1" [ngModel]="model"
        (ngModelChange)="validate($event)">`,
})
export class AppComponent {
  title = `Angular - RC.1`;
  model = 3;
  validate(value)  {
    value < 0 ? this.model = 0 : this.model = value;
  }
}

Plunker

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Input field which accept only number with maxlength

From Dev

Which annotation to use for only accept number as input?

From Dev

Validator regex pattern input accept only a number with 2-5 digits

From Dev

accept only number in input text box without using script

From Dev

validate natural input number with ngpattern

From Dev

Accept only a letter input

From Dev

Accept only numeric input

From Dev

Accept only numeric input

From Dev

Allow only 2 numbers on input type number

From Dev

jQuery validation to accept natural number from asp.net textbox

From Dev

How to check if input is a natural number in Python?

From Dev

How can I make the number input only accept a certain ammount of charaters?

From Dev

How can I make the number input only accept a certain ammount of charaters?

From Dev

Angular 2 Input NgModel converting String to number

From Dev

How to use an input type=text field to accept only digits, minus sign and decimals and nothing more with Angular

From Java

Limit input to only accept parentheses, not letters or numbers

From Dev

bootstrap form input accept text only

From Dev

Making EditText input only accept numbers not working

From Dev

Userform input field to accept ONLY minutes and seconds

From Dev

To allow input text accept only numbers

From Dev

Input accept numbers-only custom directive

From Dev

HTML input accept only numbers and email

From Dev

Text box to accept only number in C#

From Dev

phone number validation that accept only + and - signs

From Dev

Regex for accept number with only one occurrence of '%' at last

From Dev

How to correctly validate number ranges in HTML Input? (Angular 2)

From Dev

Angular2 input type number, no decimal value

From Dev

Angular2 input type number, no decimal value

From Dev

angular2 ngfor only if index is less than certain number

Related Related

  1. 1

    Input field which accept only number with maxlength

  2. 2

    Which annotation to use for only accept number as input?

  3. 3

    Validator regex pattern input accept only a number with 2-5 digits

  4. 4

    accept only number in input text box without using script

  5. 5

    validate natural input number with ngpattern

  6. 6

    Accept only a letter input

  7. 7

    Accept only numeric input

  8. 8

    Accept only numeric input

  9. 9

    Allow only 2 numbers on input type number

  10. 10

    jQuery validation to accept natural number from asp.net textbox

  11. 11

    How to check if input is a natural number in Python?

  12. 12

    How can I make the number input only accept a certain ammount of charaters?

  13. 13

    How can I make the number input only accept a certain ammount of charaters?

  14. 14

    Angular 2 Input NgModel converting String to number

  15. 15

    How to use an input type=text field to accept only digits, minus sign and decimals and nothing more with Angular

  16. 16

    Limit input to only accept parentheses, not letters or numbers

  17. 17

    bootstrap form input accept text only

  18. 18

    Making EditText input only accept numbers not working

  19. 19

    Userform input field to accept ONLY minutes and seconds

  20. 20

    To allow input text accept only numbers

  21. 21

    Input accept numbers-only custom directive

  22. 22

    HTML input accept only numbers and email

  23. 23

    Text box to accept only number in C#

  24. 24

    phone number validation that accept only + and - signs

  25. 25

    Regex for accept number with only one occurrence of '%' at last

  26. 26

    How to correctly validate number ranges in HTML Input? (Angular 2)

  27. 27

    Angular2 input type number, no decimal value

  28. 28

    Angular2 input type number, no decimal value

  29. 29

    angular2 ngfor only if index is less than certain number

HotTag

Archive