Is there a way to restrict the amount of user input that is allowed?

Is it possible to restrict the input quantity when using the built-in arrow icon in the text field, but not when typing manually?

https://i.sstatic.net/jjogP.png

<TextField variant="outlined" label="Quantity" 
                onChange={(e) => setItemName({...itemName, quantity: e.target.value})} 
                type="number"
                fullWidth name="quantity"  InputProps={{ inputProps: { min: 0, max: 10, maxLength: 2}}}
                pattern="^-?[0-9]\d*\.?\d*$"
                />

Answer №1

Here is a suggestion:

onInput={(event) => {event.target.value > 5 ? event.target.value = 5 : event.target.value}}

Answer №2

To handle this, incorporate it into your onChange function. Simply check if the value entered in the input is greater than the defined maximum value, and if so, set the input value to be equal to the max value.

<TextField
  variant="outlined"
  label="Quantity" 
  onChange={(e) => {
     const { target: { value, max } } = e;
     let inputValue = value;
     if (inputValue > max) inputValue = max;
     setItemName({ ...itemName, quantity: inputValue })
  }
  type="number"
  fullWidth
  name="quantity"
  InputProps={{ inputProps: { min: 0, max: 10, maxLength: 2}}}   
  pattern="^-?[0-9]\d*\.?\d*$"
/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

update confirmed data from a record

I am facing a unique challenge, where I have an edit form that updates a record in a table. One of the fields, let's say username, needs to be unique. To validate this, I am using the jQuery validation plugin along with the remote method as shown belo ...

Leveraging Next.js 'useClient' in conjunction with server component (global)

Hello there! I'm trying to achieve a 50% opacity effect on my Gallery when the search bar is in use. However, I'm facing challenges using 'use client' with the glob library. Here's the code snippet: app/page.tsx "use client&qu ...

Check the box to show the corresponding sections of the form

.If there are two checkboxes with options true or false, how should I handle a third checkbox? I want to display different parts of the form based on the selection of the checkboxes. ...

Automatically scrolling down a div as new content is added using XMLHTTPRequest.openConnection

https://jsfiddle.net/kv5gbamg/ - I created a jsfiddle to demonstrate the functionality of the system Essentially, I am seeking a way to automatically scroll the scrollbar to the bottom every time a new message is received. My system updates the div with ...

Customizing the text and active color of MuiButton

Currently, I am in the process of designing a Material-UI theme using the following: export const theme = createMuiTheme({ palette: { primary: { main: '#7ab800', dark: '#34b233', }, }) This setup results in a B ...

Choose a specific <div> element by its unique ID and trigger a click event on it as soon as the page loads

There is a div element on my webpage tagged with the ID of #activateeditbutton. I am hoping to have this specific div trigger a click event upon the page's initial loading. In an attempt to achieve this functionality via jQuery, I have written the fo ...

Issue: Unhandled promise rejection: BraintreeError: The 'authorization' parameter is mandatory for creating a client

I'm currently working on integrating Braintree using Angular with asp.net core. However, I've encountered an issue that I can't seem to solve. I'm following this article. The version of Angular I'm using is 14, and I have replicate ...

Is there a way to maintain the second form's state when the page is refreshed using Vue.js?

I have a challenge in creating a web-app with only 2 pages. The first page requires filling out forms across two stages, but I am unable to create separate pages for this purpose. Is there a way to display two forms on one page and remain on the second for ...

I encountered an issue while constructing a React application. An error message popped up indicating: "Warning: Can't execute a React state update on a component that is not mounted"

Having difficulty pinpointing the source of the error message displayed below. Should I focus my investigation on the specific lines mentioned in the console, such as Toolbar.js:15? Is the console indicating that the error lies there? Additionally, what i ...

Detecting if a form is in a dirty state before navigating away from a page/route using React Router v6

Here are the versions of the packages I am currently using: React version - 16.13.1 react-router-dom version - 6.0.0-beta.0 react-redux version 7.2.0 Material UI version 4.11.0 I'm looking for the best way to check if a form is dirty (has been change ...

Exploring the possibilities of custom layouts for specific routes within the pages directory in Next.js

I am interested in incorporating layout-based routing within my project's pages directory. I want to find a way to have a specific file, like the _app.tsx, that can only affect the files located inside a particular folder. This setup would operate si ...

Exploring the functionality of componentWillReceiveProps within functional components

Embarking on my journey with functional components after extensively working with class components. While experimenting, I encountered a challenge: how can I incorporate the functionality of componentWillReceiveProps within the context of the useEffect h ...

"Turn a blind eye to Restangular's setRequestInterceptor just this

When setting up my application, I utilize Restangular.setRequestInterceptor() to trigger a function that displays a loading screen whenever a request is made with Restangular. Yet, there is a specific section in my application where I do not want this fun ...

Animating Array of Paragraphs with JQuery: Step-by-Step Guide to Displaying Paragraph Tags Sequentially on Each Click

var phrases = ['phraseone', 'yet another phrase', 'once more with feeling']; $(".btn").on('click', function() { for(var i=0; i < phrases.length; i++) { container.innerHTML += '<p>' + ...

Tips for personalizing the weekday title of Material-UI's DateRangePicker

I've been struggling for hours trying to customize the weekday titles in Material-ui DateRangePicker. Instead of displaying "S", "M",..., I want to show the titles as I desire. I attempted to customize the dateAdapter without succe ...

Error: Unable to access property 'addEventListener' of null. This issue appears to be related to a Chrome extension

It's been frustrating dealing with this persistent error. I'm in the process of creating my very first chrome extension and for some reason, I just can't seem to pinpoint what's wrong with this particular code snippet: let beginButton = ...

In JavaScript, implement event listeners exclusively on the main container and its immediate child elements

Is there a way to target just the main container and its second child elements for an event? Specifically, targeting id="container" and all elements with class="secondChild" Currently, I have a listener attached to all elements inside ...

Minifying Angular using grunt leads to 'Error initializing module' issue

Currently, I have multiple controllers, models, and services set up as individual files for preparation before minification. My goal is to combine and minify all these files into one JS file for production. To illustrate how my files are structured, here ...

Vanish Dropdown upon clicking Using JavaScript

Hey, I'm new to web development and I'm currently working on creating a web app for iPhone. Everything is going smoothly except for one issue - my dropdown menu works perfectly on desktop Chrome, but on the iPhone's Safari browser, I can&ap ...

"Hey, do you need a see-through background for your

Currently, I am implementing the JS library found at . Unfortunately, I am struggling to make the background transparent or any other color. It appears that the issue lies in the fact that the tab styles are being overridden by the JS library whenever the ...