0

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 
DhanaLaxshmi
  • 424
  • 1
  • 11
  • 24
  • min="1" should work fine, as an alternative you can add a condition under your click function to reject the data and show an error but again, you are doing something fundamentally wrong. Can you share the whole code with min="1" in place? I do not see that added to your current code. – Shashi Aug 11 '22 at 05:23

1 Answers1

-1

Add min="0" attribute in min tag.

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
  • Please check i have tried already – DhanaLaxshmi Aug 11 '22 at 05:29
  • Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Aug 16 '22 at 13:29