Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get invalid value from input type number

I am using input type number. How can I get the value from this when its not valid. For example using type number and printing just 'e' thats not valid by itself.

I am using 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>
like image 600
pethel Avatar asked Oct 16 '16 17:10

pethel


People also ask

How do I allow only input field numbers?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.

What is e in input type number?

The E stands for the exponent, and it is used to shorten long numbers. Since the input is a math input and exponents are in math to shorten great numbers, so that's why there is an E.


2 Answers

According to my findings there is no solution to this specific problem.
The only way to get it is to set the input as type="text" and decide within the function about the validity:
(source: Validate decimal numbers in JavaScript - IsNumeric())

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

You have to call the onChange JS Function in this way onChange="onChange(this)" and use event.value instead of event.target.value in order to get the correct result.

function onChange(event) {
  console.log(event.value)
}
<form novalidate>
  <input type="number" onChange="onChange(this)">
</form>
like image 68
Luca Jung Avatar answered Nov 04 '22 11:11

Luca Jung


This is actually possible (at least in Chromium-based browers like Chrome and Edge), it's just a pain. You can get it via the selection interface:

const input = /*...the input...*/;
input.select();
const text = getSelection().toString();

No luck on Firefox, sadly.

Live Example:

const theInput = document.getElementById("the-input");
const theButton = document.getElementById("the-btn");

function saveSelection() {
    const selection = getSelection();
    const range = selection.rangeCount === 0 ? null : selection.getRangeAt(0);
    return range;
}

function restoreSelection(range) {
    const selection = getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
}

function getRawText(input) {
    const sel = saveSelection();
    input.select();
    const text = getSelection().toString();
    restoreSelection(sel);
    return text;
}

theButton.addEventListener("click", event => {
    const value = theInput.value;
    const text  = getRawText(theInput);
    console.log(`value = "${value}", but text = "${text}"`);
});
<p>
Copy some invalid numeric text (like <code>9-9</code>) and paste it into this field:
</p>
<input type="number" id="the-input">
<p>
Then click here: <input type="button" id="the-btn" value="Go">
</p>
like image 40
T.J. Crowder Avatar answered Nov 04 '22 11:11

T.J. Crowder