How to no allow negative decimal and 0 value to an input type number in React JS

988 views Asked by At

I have a input box which should not allow negative decimal value and 0 trying to do using charcode as shown below

  <input type="number" min="1"  name="minValue" 
      className="input-box removeNumberArrow"  value={this.state.minValue}
      onChange={this.handleMinChange} onKeyPress={this.validateEntredVal}/>
   

Below method is called when ever a key is pressed

validateEntredVal = (event) => {
        let charCode = (evt.which) ? evt.which : evt.keyCode;
  if ((charCode > 31 && (charCode <48 || charCode > 57)) && charCode !== 46) {
    evt.preventDefault();
  } else {
    return true;
  }
    };

Using the above method it will not allow the characters and negative values. problem it should not allow 0 and decimal as well (means the text box value should always accept value greater than zero) Please help on the same

min="1" is not working 
1

There are 1 answers

2
19DCE146 JEEL VACHHANI On

Add min="0" attribute in min tag.