DEV Community

Narender Saini
Narender Saini

Posted on

How to block +,- and e in Number Input

How to block +,- and e in Number Input

Have you ever used number input and added special logic on onChange event to prevent +, – and e charterers saving to state. In this article we will gonna learn how to write a util function to block these +, – and e in your number input.

Problem

Let’s see below example.



import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const [value, setValue] = useState("");
  return (
    <div className="App">
      <input
        type="Number"
        value={value}
        onChange={({ target: { value } }) => {
          setValue(value);
        }}
      />
    </div>
  );
}



Enter fullscreen mode Exit fullscreen mode

If you notice we are using a number input and it’s also working fine but we don’t want user to enter +, – and e while typing.

How to block +,- and e in Number Input

Solution

The solution is very straight forward. We will gonna write a reusable util function which basically gonna block invalid characters.



export const blockInvalidChar = e => ['e', 'E', '+', '-'].includes(e.key) && e.preventDefault();



Enter fullscreen mode Exit fullscreen mode

We can simply import this function and use it on onKeyPress event.



import React, { useState } from "react";

import { blockInvalidChar } from "./blockInvalidChar";
import "./styles.css";

export default function App() {
  const [value, setValue] = useState("");
  return (
    <div className="App">
      <input
        type="Number"
        value={value}
        onKeyDown={blockInvalidChar}
        onChange={({ target: { value } }) => {
          setValue(value);
        }}
      />
    </div>
  );
}



Enter fullscreen mode Exit fullscreen mode

How to block +,- and e in Number Input

Edit k63s4

How to use Formik with yup in React

Top comments (8)

Collapse
 
vanaf1979 profile image
Stephan Nijman

I'm not sure if this is a problem in React, but in my experience with vanilla js and the onkeydown event it sometimes misses the last typed character (In some browsers). If you see this poblem just use the onkeyup event instead! :)

Collapse
 
itsjzt profile image
Saurabh Sharma

I used to cast value to number explicitly using Number(e.target.value).

Collapse
 
wysher profile image
Mariusz Wachowski

When using onKeyDown user cannot use many keys like:

  • TAB (move caret to next input)
  • ALT+A (selecting all)
  • CTRL+C/CTRL+V/CTRL+X
  • arrows (up/down to add/subtract one, left right to move caret)
  • home, end, page up, page down to move caret
  • etc.

You should rather check value onChange.

Collapse
 
sozonome profile image
Agustinus Nathaniel

This is simple and useful!

Collapse
 
thakurujjwal profile image
Ujjwal Thakur

I used this, and it worked perfectly.

Collapse
 
ccalvarez profile image
Carolina

Could you try this on mobile please? On mobile it keeps accepting plus, minus and dot characters.

Thanks.

Collapse
 
harshmalviya72 profile image
Harsh Malviya

This works fine when user is entering the value using keyboard.
But when the user scrolls the mouse on input this doesn't work. Is there a way to block that as well?

Collapse
 
vinayprakash profile image
vinay • Edited

Add this -- >

const numberInputOnWheelPreventChange = (e: any) =>
{
// Prevent the input value change
e.target.blur();
// Prevent the page/container scrolling
e.stopPropagation();
// Refocus immediately, on the next tick (after the current
// function is done)
setTimeout(() => {
e.target.focus();
}, 0);
};