Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
not entering special characters on user input

I have an input field where i am trying to block the special characters [dot, hiphen, space comma and e] so that user can't enter this special characters on both mobile and chrome.

function App() {
  const handleInput = (event) => {
    event.target.value = event.target.value.replace(/[e\.\- ]/g, "");
  };

  return ( <
    div className = "App" >
    <
    input type = "number"
    onInput = {
      handleInput
    }
    /> < /
    div >
  );
}

// Render it
ReactDOM.render( <
  App / > ,
  document.getElementById("react")
);
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Facing two issues on this, one is (999.) I am able to enter dot in between numbers, also when I enter some numbers and type hiphen then its clearing all the numbers (999-) => ().

How can I stop this two behaviour so that user can enter only 0-9 digits

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Replacing the value when the user is typing makes for poor UX (the cursor jumps around). In your previous questions you were preventing those characters via keydown, which seems like the better approach. It may not work on all mobile browsers, but since you presumably want the numeric on-screen keyboard they provide for type="number", you may have to accept that and so some post-processing when you're going to use the value (rather than as they're typing it).

Here's an example of that pragmatic approach, preventing where you can, post-processing the value when using it in case you weren't able to prevent the invalid chars on some mobile devices. I've also made the input a controlled input since that makes it easier to use its value elsewhere in the component:

const {useState} = React;

const invalid = new Set(["e", ".", "-", " "]);
function App() {
    const [value, setValue] = useState("");
    
    const handleInput = (event) => {
        if (invalid.has(event.key)) {
            event.preventDefault();
        }
    };
    
    const useValue = (event) => {
        const prepped = value.replace(/[e.\- ]/g, "");
        console.log(`Using value "${value}"`);
    };
  
    return (
        <div className="App">
            <input
                type="number"
                onKeyDown={handleInput}
                onChange={e => setValue(e.target.value)}
                value={value}
            />
            <input type="button" value="Use Value" onClick={useValue} />
        </div>
    );
}

ReactDOM.render(<App />, document.getElementById("root"));
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!