Min and max value of input in angular4 application

后端 未结 9 1090
温柔的废话
温柔的废话 2020-12-08 13:01

I have an angular4 application with a form. In this one I have an input to enter a percentage. So, I want to block the input with value between 0 and 100. I tried to add

相关标签:
9条回答
  • 2020-12-08 13:43

    Simply do this in angular2+ by adding (onkeypress)

    <input type="number" 
        maxlength="3" 
        min="0" 
        max="100" 
        required 
        mdInput 
        placeholder="Charge" 
        [(ngModel)]="rateInput"
        (onkeypress)="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57"
        name="rateInput">
    

    Tested on Angular 7

    0 讨论(0)
  • 2020-12-08 13:45

    [I assume the reader has basic knowledge of Angular2+ and Forms]

    It is easy to show a numerical input and put the limits, but you have to also take care of things may happen out of your predictions.

    1. Implement the tag in your 'html':
    <input type="number" [min]="0.00" [max]="100.00" [step]="0.01" formControlName="rateFC">
    
    1. But as Adrien said, still user can enter manually a wrong number. You can validate input by Validators easily. In your '.ts':
    import { FormGroup, FormControl, Validators } from '@angular/forms';
    
    //many other things...
    
    this.myFG = new FormGroup({
       //other form controls...,
       rateFC  : new FormControl(0, [Validators.min(0), Validators.max(100)])
    });
    
    1. Up to now everything is ok, but it is better to let the user know the input is wrong, then draw a red line around the invalid input element by adding to your style:
    .form-control.ng-touched.ng-invalid{
        border:2px solid red;
    }
    
    1. And to make it perfect, prevent the user to submit the wrong data.
    <button type="submit" [disabled]="!myFG.valid">Submit</button>
    
    0 讨论(0)
  • 2020-12-08 13:47

    Most simple approach in Template driven forms for min/max validation with out using reactive forms and building any directive, would be to use pattern attribute of html. This has already been explained and answered here please look https://stackoverflow.com/a/63312336/14069524

    0 讨论(0)
  • 2020-12-08 13:50

    You can control with a change event if the input is within your range, if it is not in the range you assign 0.

    <md-input-container>
      <input type="number" 
        maxlength="3" 
        min="0" 
        max="100" 
        required 
        mdInput 
        placeholder="Charge" 
        [(ngModel)]="rateInput"
        (change)= "rateInput < 0 ? rateInput = 0 : rateInput; rateInput > 100 ? rateInput = 0 : rateIntput;"
        name="rateInput">
      <md-error>Required field</md-error>
    </md-input-container>
    
    0 讨论(0)
  • 2020-12-08 13:51

    Here is the solution :

    This is kind of hack , but it will work

    <input type="number" 
    placeholder="Charge" 
    [(ngModel)]="rateInput" 
    name="rateInput"
    pattern="^$|^([0-9]|[1-9][0-9]|[1][0][0])?"
    required 
    #rateInput2 = "ngModel">
    
    <div *ngIf="rateInput2.errors && (rateInput2.dirty || rateInput2.touched)"
        <div [hidden]="!rateInput2.errors.pattern">
            Number should be between 0 and 100
        </div>
    </div>
    

    Here is the link to the plunker , please have a look.

    0 讨论(0)
  • 2020-12-08 13:55

    I succeeded by using a form control. This is my html code :

        <md-input-container>
            <input type="number" min="0" max="100" required mdInput placeholder="Charge" [(ngModel)]="rateInput" name="rateInput" [formControl]="rateControl">
            <md-error>Please enter a value between 0 and 100</md-error>
        </md-input-container>
    

    And in my typescript code, I have :

            this.rateControl = new FormControl("", [Validators.max(100), Validators.min(0)])
    

    So, if we enter a value higher than 100 or smaller than 0, the material design input become red and the field is not validate. So after, if the value is not good, I don't save when I click on the save button.

    0 讨论(0)
提交回复
热议问题