1

Is it possible to show a string within an input field of the type number?

I want to do something like this:

<input value="4'000" placeholder="Type some numbers" type="number" />

It will always show the placeholder because "4'000" has an apostrophe and is, therefore, a string.

Any ideas on how to show a string (if possible without package - I'm using React) inside a number typed input?

Thanks!

2
  • Can you share any use case of it? There are many questions revolving around it. If you really need a number then why apostrophe? If you need a string, then is it mandatory to have the input type as a number, you could text as input type? So if you can share the use case then it will be much better to tell anything. Commented Mar 1, 2021 at 7:08
  • @not-a-bot Actually only because of readability purpose. Commented Mar 1, 2021 at 7:31

3 Answers 3

1

From WDN Web Docs input type="number"

input elements of type number are used to let the user enter a number. They include built-in validation to reject non-numerical entries.

Sou you can't show a string inside a number typed input.

But you could transform "4'000" to '4000', and show it as a number:

"4'000".replaceAll("'", "") // => "4000"

<input value="4000" placeholder="Type some numbers" type="number">
Sign up to request clarification or add additional context in comments.

2 Comments

Alright. Thanks. Haven't seen that. I already have a number and want to format it and display it a bit prettier, but if this is not possible it's alright.
For display there's a workaround to do this by use <input type="text"> HTML Input type number Thousand separator
1

So before you add the value on your value field, what you can do is "process" it first to remove all non numeric stuff on the value:

use this function: \D is a shorthand regex that matches all non-digit.

const stripNonNumeric = str => str.replace(/\D/g,'');

(If you are using state)

const [val, setVal] = useState(stripNonNumeric(props.defaultValue));

const onChangeValue = (e) => {
   // this should be unnecessary if the field is already type="number",
   // but if its not then this use this, else just use e.target.value
   const value = stripNonNumeric(e.target);

   setVal(value);
};

return <input type="number" value={val} onChange={onChangeValue} />;

If you are getting your value from somewhere else:

const value = props.valFromSomewhere;

return <input type="number" value={stripNonNumeric(value)} />;

8 Comments

Yeah I already have a numeric state. I want to show it with some apostrophes because of readability but how the answer of @eux says, this is not possible.
@RobinAegerter then just use a string type input and just process the value to make sure its a number (just like my first example) what's wrong with that?
@ia-am-l with a text input you don't have the advantage of the two arrows at the right of the input field and it's not the correct keyboard for mobile devices. And you also have to write your own validation ;)
@RobinAegerter I mean, you can just create 2 up and down buttons that will mimic that behavior. logic shouldn't be that hard for that one as for validation I already provided you the logic. But if you really want to use type=number, then you can't. what I'm suggesting is an alternatives that you can do if you really want to fix your issue.
@ia-am-l thanks. Yeah that would be an idea. But with this solution you can't solve the keyboard layout problem on mobile phones because it is not possible to set the layout from the web.
|
1

It will be much better to use input type as number for the values which are rational/integers but not strings.

So in your case, you can use text as an input field for better readability and in javascript you can convert that string to a number.

let num = document.getElementById("some-number").value;

num = num.replace(/\'/g,'');
num = parseInt(num,10);

console.log(num);
<input type="text" value="4'000" id="some-number" />

4 Comments

how I described in this comment there is still the problem with the keyboard layout.
@RobinAegerter number type will never allow you to have some other characters which are not related to numbers.
Yeah now I know. I'll just leave it there (without any apostrophe). But thanks!
@RobinAegerter See this answer it might guide a little to achieve what you wish for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.