最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get invalid value from input type number - Stack Overflow

programmeradmin11浏览0评论

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>

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>
Share Improve this question asked Oct 16, 2016 at 17:13 pethelpethel 5,53714 gold badges58 silver badges89 bronze badges 4
  • You can't, e is allowed because it refers to exponents. you <input type="text"> instead and listen to changes using the input event. You can allways convert for number using parseFloat or just the unary +. – Noctisdark Commented Oct 16, 2016 at 17:23
  • @Noctisdark, thanks. I understand the reason i can write e in that field. The reason I need to capture is for validation message. I am creating a chat soo I need to write back exactly what the user typed. – pethel Commented Oct 16, 2016 at 20:25
  • 1 @pethel did you find a solution – Abhi Commented Apr 19, 2019 at 3:57
  • You may have already checked this one stackoverflow.com/questions/18852244/… and I was trying use <input type="number" id="myInput" onChange="onChange(this.id)"> but this is also failed. – Harshana Commented Jun 24, 2019 at 16:59
Add a comment  | 

3 Answers 3

Reset to default 4

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>

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>

I don't really get you, but I think you're looking to get the input value. You're looking to target the value, but your tag doesn't have this attribute. In this case you need to add value ={this.state.value} to the input tag as a prop and add the new state in the constructor. this.state = {value: ''}

发布评论

评论列表(0)

  1. 暂无评论