Input type number validation

input type=number decimal
input type=number validation in javascript
input type=number remove arrows
html input numbers only
input pattern numbers only
input type=number maxlength
input type=number'' min max validation
input type=number default value

I have input type number, I want to restrict it to only numbers between 1 and 2. It can accept numbers between 0.00001 and 2 with decimal places up to 5 and not 0.

How do i do it? And also the user must not be able to write anything else manually.

I have tried min="1" and max="2" attributes but user is still able to type 0 manually and form is being submitted.

My project is based on angular 5 and I am using a template driven form. Please do not suggest solutions for reactive forms.

You can try regex also

If you want number in range of 1-2 with 5 decimals without number 2 then use pattern below,


And if you want less than and equal to 2, you can set pattern as,

^(?:2|2.0|2.00|2.000|2.0000|2.00000 | (.[0]{1,2})?|([1])(.[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)?)$

For example:

<input class="form-control" 
           [(ngModel)]="data" name="dataModel" #dataModel="ngModel" required
           pattern="^(?:([2])(\.[0]*)? | (\.[0]{1,2})?|([1])(\.[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)?)$">
    <div *ngIf="dataModel.errors && (dataModel.dirty || dataModel.touched)">
      <small [hidden]="!dataModel.errors.required">
      <small [hidden]="!dataModel.errors.pattern">
        pattern check

And if you want in range of 0-2 including 2 then you can set pattern as,

^(?:2|2.0|2.00|2.000|2.0000|2.00000 | (.[0]{1,2})?|([0-1])(.[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)?)$

If you want in range of 0-2 excluding 0 then pattern can be (this pattern accepts from 0.00001 to 2.00000 numbers),


HTML input type="number", You can use the step attribute to constrain valid values to a certain set of steps (e.g., multiples of 10). I want to restrict entry of input onto a field of type number such that input cannot be outside range of min-max specified in the html. input type = "number" min = "1" max = "5". Is there a way of outputting the number field without the text box and i would rather not use.

i have tried min="1" and max="2" attributes. but user is still able to type 0 manually and form is being submitted.

you can use keydown event here (keydown)="false" this will prevent the user to type anything, and still he will be able to use the arrow controls to increase and decrease the numbers. I would suggest you to disable paste option too (paste)="false" to prevent paste in the input field.

   <input type="number" step="0.00001" max="2" min="1" (keydown)="false" (paste)="false" required/>

HTML5 Form Validation Examples < HTML, This seems incredibly short-sighted since it will only be a useful validator when your database fields are signed floating-point numbers (for  input elements of type tel are used to let the user enter and edit a telephone number. Unlike input type="email" and input type="url", the input value is not automatically validated to a particular format before the form can be submitted, because formats for telephone numbers vary so much around the world.

Try this.

<input type="number" step="0.00001" max="2" min="1" [(ngModel)]="data" #value="ngModel" required/>

<div *ngIf="value.invalid && (name.dirty || name.touched)"
     class="alert alert-danger">

  <div *ngIf="value.errors.required">
    Value is required.
  <div *ngIf="value.errors.min">
    Should not be less than 1
  <div *ngIf="value.errors.max">
    Should not be greater than 2


And validate your field for min and max values and display a error message.

HTML5: number input type that takes only integers?, You can try regex also. If you want number in range of 1-2 with 5 decimals without number 2 then use pattern below,. [1]+(. INPUT type="number" and type="range" The number and range input types also accept parameters for min, max and step. In most cases you can leave out step as it defaults to 1. Here you see an example including both a number input, typically displayed as a 'roller' and a range input displayed as a 'slider':

You can trigger a function for every keypress event and write your validation logic such that the user can enter only valid values.

<input type="number" step="0.00001" max="2" min="1" (keydown)="filterValue($event)" (paste)="false" required/>

Function :

  var filterValue = function($event){
     var keys={ 'up': 38,'right':39,'down':40,'left':37,                      
                   for(var index in keys) {
                            if (!keys.hasOwnProperty(index)) continue;
                            if ($event.charCode==keys[index]||$event.keyCode==keys[index]) {
                                return; //default event

Define only required values in the keys object so that user will not be allowed to enter other values.

Input type number validation, Brad argues that we then shouldn't be using <input type="number"> for at a while back in a post all about constraint validation in HTML. Definition and Usage. The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed. min - specifies the minimum value allowed. step - specifies the legal number intervals. value - Specifies the default value.

You probably don't need input type=“number”, Validating Numbers. The number input type only accepts numbers. Browsers will either refuse to accept letters and other characters, or alert  Input Type Number. The <input type="number"> defines a numeric input field. You can also set restrictions on what numbers are accepted. The following example displays a numeric input field, where you can enter a value from 1 to 5:

Form Validation Part 1: Constraint Validation in HTML, Due to this, the tel input doesn't attempt to validate the format of a phone number. You can make use of the associated validation tools such as the  By default, the number input type only validates if the number is an integer. To allow float numbers, specify step="any" . If omitted, the step value defaults to 1 , meaning only whole numbers are valid.

HTML5 Input Types: Where Are They Now?, Text input with number validation and transformation. Sets the number validation error if not a valid number. The model must always be of type number otherwise  The pattern attribute specifies a regular expression that the <input> element's value is checked against on form submission. Note: The pattern attribute works with the following input types: text, date, search, url, tel, email, and password. Tip: Use the global title attribute to describe the pattern to help the user.

  • You can set calidation and show error accordingly
  • PardeepJain meant 'validation'.
  • If you want number in range of 1-2 with 5 decimals without number 2 i want to include 2 also, basically from 0.00001 to 2.
  • @Sushmit So you can go for second pattern
  • @Sushmit if you want between 0 and 2 then use pattern ^(?:2|2.0|2.00|2.000|2.0000|2.00000 | (.[0]{0,2})?|([1])(.[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)?)$
  • so it will not accept if number is less than 0 right ?
  • Yes, it will just accept number with 5 decimal digits within range 0-2
  • but it will be painful if user has to type 0.0001 or something similar by using arrows.
  • yeah it will be :\
  • it is saying identifier min is not defined. __type does not contain such a member
  • will this work for copy paste and in mozilla, IE11 also?