Search
 
SCRIPT & CODE EXAMPLE
 

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>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: divide intagers javascript 
Javascript :: Writing files in Node.js 
Javascript :: how to fill false into array javascript 
Javascript :: javascript fetch json 
Javascript :: set dropdown value using jquery 
Javascript :: slick slider infinite loop 
Javascript :: refresh page after success ajax 
Javascript :: javascript loop thrown object 
Javascript :: nodejs strict ssl false 
Javascript :: visual studio appsettings development json not nested appsettings.json 
Javascript :: npm update react 
Javascript :: JavaScript Regex - Remove Whitespace from Start and End 
Javascript :: js style remove property 
Javascript :: cypress find aria-label 
Javascript :: how to get window size in react js 
Javascript :: file input disable open file picker javascript 
Javascript :: Uncaught TypeError: this is undefined ApolloClient ApolloClient.ts:72 
Javascript :: angular schematics datatable 
Javascript :: useeffect not working with react-dom-router 
Javascript :: jquery select radio 
Javascript :: js get first 3 characters of string 
Javascript :: js add click listener 
Javascript :: cnpj pattern js 
Javascript :: select only jpg jpeg images 
Javascript :: add an element to the front of an input list in javascript 
Javascript :: vue redirect to route 
Javascript :: regular expression to validate if the given input is valid Indian mobile number or not 
Javascript :: express js params 
Javascript :: load js after ajax 
Javascript :: js find node number in div 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =