Comments (4)
In the example you provided, you are using React to create a text input component that converts the entered text to uppercase. However, you have noticed that this can interfere with the native HTML validation, especially when the input does not meet validation requirements, as in the case of "a". This happens because the actual value of the input is not altered; only the value displayed in the user interface is changed.
To work around this issue, you can use the onInput event instead of the onChange event. The onInput event is triggered whenever the value of the input field is modified, whereas the onChange event may be triggered after the input field loses focus, which is not ideal for real-time validation.
from react.
@felipe-rodolfo React's onChange
handler is actually an onInput
handler, React doesn't have have an actual onChange
handler. So they behave the same way.
Also if onChange
handler would have worked like you described - my playground would work correctly since handler wouldn't fire while I'm typing stuff into the input until I unfocus/press enter etc. That's not what happens for the reasons described above.
from react.
You are correct, and I apologize for any confusion. React's documentation does indicate that onInput
fires immediately when the value is changed by the user, and it is considered idiomatic in React to use onChange
instead, as it works similarly.
In most cases, using onChange
in a controlled component is the appropriate choice, and it should behave as expected for real-time input handling. The issue you were experiencing with the validation may be specific to how transformations were applied in your onChange
handler.
To address the validation issue you initially described, you may need to adjust how you handle transformations in the onChange
handler or consider other approaches. Using onChange
itself is not the problem; it's about making sure your transformations and validation logic are compatible with the controlled component's behavior.
to solve the validation issue when converting text to uppercase while still using the onChange event, you can follow this approach:
Keep using the onChange event to update the component state and convert the text to uppercase.
Add a custom validation function that checks if the value meets the requirements, such as the minimum length, within the onChange event.
Use setCustomValidity to set up custom validation based on the established rules.
Here's a modified code example:
import React from 'react';`
export function App(props) {
const [text, setText] = React.useState('');
const handleInputChange = (e) => {
const inputText = e.target.value.toUpperCase();
setText(inputText);
// Custom validation function
const isValid = inputText.length === 6;
if (isValid) {
e.target.setCustomValidity('');
} else {
e.target.setCustomValidity('The text must have 6 characters.');
}
};
return (
<div className="App">
<input
value={text}
onChange={handleInputChange}
minLength={6}
maxLength={6}
required
/>
<div>{text}</div>
</div>
);
}
from react.
Could you please not clutter the issue with chatGPT responses?
from react.
Related Issues (20)
- Bug: Search functionality not working correctly on react.dev in Safari on iOS real device (iPhone 12 Pro) HOT 2
- Bug:
- Google Translate causing "Text content does not match server-rendered HTML" HOT 1
- Bug: useFormState formAction becomes null in strict mode HOT 1
- Bug: Source is not showing in 5.0.2 HOT 9
- Question and suggest: The official identifier of Server Compnoent and Client Component HOT 3
- Bug: when installing the application, the old version of "react-scripts" is installed HOT 1
- Bug: Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string. HOT 4
- Bug: optimistic state (useOptimistic) shows both optimistic and returned from server data when running several async actions HOT 6
- [DevTools Bug]: React Profiler reports higher hook numbers than shown in Components HOT 2
- about the react performance (how to improve it) HOT 3
- Bug: use() hook HOT 4
- Bug: Flight (RSC) examples are not accessible HOT 1
- useMemo reruns when the dependencies not changed when memoised value is a fn HOT 3
- Bug: [Flight] Async server components in `ai/rsc` not rendered correctly HOT 1
- Bug: React Dev tools extension showing wrong source file path on inspection HOT 4
- Bug: Using the hook useDeferredValue - Suspense fallback didn't show the contents HOT 4
- Bug: react-intl formatMessage cannot format message with param that neat by Single quotation mark HOT 3
- custom use directives + using closure serializer
- In react fiber, is didReceiveUpdate related to dirty checking?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.