Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

pass only numbers in input react js

import React, { useState } from "react";

export default function App() {
  const [val, setVal] = useState(0);

  return (
    <div>
      <input
        type="text"
        pattern="[0-9]*" //set pattern
        value={val}
        onChange={(e) =>
          setVal((v) => (e.target.validity.valid ? e.target.value : v)) //check if the pattern equals to the value
        }
      />
    </div>
  );
}
Source by thewebdev.info #
 
PREVIOUS NEXT
Tagged: #pass #numbers #input #react #js
ADD COMMENT
Topic
Name
4+4 =