3

I would like to know how to set a min and max number in angular2 when we try to limit an input text it's very simple we have to put any ideas about limiting the number for exemple 3 numbers

it work perfect on type text but with number I think we have to do a directive

3
  • If you are using Validators class to perform validation, you will need to write custom validators. Pleast post more code so I can see the way you perform validation. Commented Aug 29, 2016 at 13:09
  • <input type="number" [(ngModel)]="myvar" name="name" minLength="0" maxLength="4" required /> Commented Aug 29, 2016 at 13:26
  • You didn't post enough code, I wouldn't have time to look through it anyway, but this link was very useful to me, I also needed to do same thing you want to do: coryrylan.com/blog/… Commented Aug 29, 2016 at 13:36

2 Answers 2

5

Angular provide some Validators like required,maxLength.. but you can create your own Validator like this :

  1. Create a fonction (this one take your control value, check if it's between 0 and 100. If it's okay, it returns null, which means no problems, otherwise you return an object with valid to false)
function validateNumber(c: FormControl) {
  return c.value > 0 && c.value < 100 ? null : {valid: false}
};
  1. Put your function as a Validator in your control : new FormControl('', validateNumber)
Sign up to request clarification or add additional context in comments.

3 Comments

the min="1" max="100" doesn't work, if I put 100000 for exemple
I've updated my answer, it's more related to your question i think
Great. works perfect. if you want to add validation without deleting any exsiting one, use this: _fc.setValidations(validateNumber); (_fc is a FormControl).
0

You also can create some directives to manage your validator.

HTML :

<input type="number" maxNumber="999"[(ngModel)]="element.orderItemNewCredit" #newCredit="ngModel">
<div *ngIf="newCredit.errors && (newCredit.dirty || newCredit.touched)" class="alert alert-danger">
    <div [hidden]="!newCredit.errors.maxNumber">New credit cannot be superior than 999/div>
</div>

Directive : maxNumberValidator.directive.ts

import {Directive, Input} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, ValidationErrors, Validator} from "@angular/forms";

@Directive({
    selector: '[maxNumber]',
    providers: [{provide: NG_VALIDATORS, useExisting: MaxNumberValidatorDirective, multi: true}]
})

export class MaxNumberValidatorDirective implements Validator {
    @Input() maxNumber: number;

    constructor() { }

    validate(control: AbstractControl): ValidationErrors {
        return control.value <= this.maxNumber ? null : { maxNumber: { valid: false } };
    }
}

And do not forget to add this directive into your module file.

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.