Get invalid value from input type number

I am using input type number. How can I get the value from this when it is not valid. For example, using the type number and printing only "e", which is not valid in itself.

I use React, but I think this question is very general.

onChange(event) {
  console.log(event.target.value) //is empty string when not using number
}

<form novalidate>
  <input type="number" onChange={this.onChange}>
</form>
+8
source share
2 answers

I do not quite understand you, but I think you are looking to get the input value. You are looking for a target value, but your tag does not have this attribute. In this case, you need to add value ={this.state.value}as a property to the input tag and add a new state to the constructor.this.state = {value: ''}

0
source

.
- input type="text" :
(: JavaScript - IsNumeric())

function onChange(event) {
  if(!isNaN(parseFloat(event.value)) && isFinite(event.value)){
    console.log("It numeric: " + event.value);
  }
  else {
    console.log("It not numeric: " + event.value);
  }
}
<input type="text" onChange="onChange(this)">
Hide result

onChange JS onChange="onChange(this)" event.value event.target.value, .

function onChange(event) {
  console.log(event.value)
}
<form novalidate>
  <input type="number" onChange="onChange(this)">
</form>
Hide result
0

Source: https://habr.com/ru/post/1657917/


All Articles