score:1
This is my solution. I handle when the card will display by check if the input has already 4 characters length and then add space
in it.
The important part is when you type each character you have to remove all old space
before add space
again otherwise it will fill the input with a lot of weird space
.
Here is my code. You can see its action here
import React, { useState } from 'react';
function App() {
const [card, setCard] = useState('')
const handleCardDisplay = () => {
const rawText = [...card.split(' ').join('')] // Remove old space
const creditCard = [] // Create card as array
rawText.forEach((t, i) => {
if (i % 4 === 0) creditCard.push(' ') // Add space
creditCard.push(t)
})
return creditCard.join('') // Transform card array to string
}
return (
<div>
<input
value={handleCardDisplay()}
onChange={(e) => setCard(e.target.value)}
/>
</div>
)
}
export default App;
Source: stackoverflow.com
Related Query
- How to display in input textbox of credit card number the 4 numbers then space?
- How to allow only numbers in textbox and format as US mobile number format in react js? ex : (224) - 5623 -2365
- How to limit the text filed length with input type number in React JS and prevent entry of E,e, -, + etc
- MUI Select | Change how the selected value display in Input
- How to limit the input number to max of two decimals with Ant Design?
- How to input decimal numbers and auto format with thousand separators at the same time in javascript?
- How to construct an Array to first compare and then add the input
- How do I restrict the input to only number at the input box?
- How do I display what I've written in the text input field upon clicking button?
- How can I take the previous 5 values I am passing through search and then using it in API. I want to display pervious five values
- How to display sum of the values entered in input box in react.js?
- How to check If the value of an input is number or string in react js?
- How to convert a div to input and then the input into div (onclick)
- How to get label values dynamically and adding the numbers together from different input with reactjs
- React display percentage next to the number input or vice versa
- Get number from the input text and then increment when the button is clicked ReactJS
- How can I check if login is successful, then display the protected routes in react hooks
- How to Display Ticks and Labels for Month Numbers for 4 years in the X-Axis using Recharts
- How can render the same card with different values based on user input in a form?
- How to display the child Grids inside the container Grid to the next line retaining the space and wrapping the grid according to the text?
- how to validate date based on input type text and display the day according to input date in react
- how to limit the number of results for autocomplete input / ant.design
- How to do so that my field must at least be able to contain only numbers and a + in front of the number (if the user wishes)
- How to limit number with 0-999 and just three numbers after the comma in React
- How to display a specific value from JSON object. Example i wanted to display the value of Low and then assign to a variable in React.js
- How to let the user input number and increase it with a button in React JS
- How to display dynamically the count of number of characters below the TextField (Inline)
- How do I get the previous value of a field in React? I want to use it to display the user's previous input when they click 'edit' on the field
- I want to get the latest value of input type Number And then pass it to the dispatch function but setState takes some time to update ? Details Below
- How to display a set number of jsx elements depending on number placed in an input field. React
More Query from same tag
- React 'String' prop passing down as an 'object'
- I don't understand how react router works
- React/Redux foreach data
- Execute Vanilla JS Scripts in React after DOM Render
- Loading a local React app inside WKWebView iOS
- Problem with React "Cannot read property 'addEventListener' of null"
- How do you update your project to use the latest version of javascript?
- Docker React App: 404s will fallback to /
- Made custom checkbox in React
- Scrolling without moving main content
- drop shadow not showing when scrolled in react
- How to pass props/state through Link and Route - react.js
- React - how to set inline CSS for each element of map function?
- Google Map Api Marker not showing with Reactjs
- Material-table TypeError: Cannot add property tableData, object is not extensible
- Cannot read properties of undefined (reading 'ids') , while using globalSelectors,simpleSelectors of redux toolkit in createEntityAdapter
- Mocking dayjs with Jest
- How to call a action creator in a functional component with react-redux?
- Uncaught Invariant Violation: DetailPage.render()
- Context.provider throw error when wrapping my app component with it
- react previous state not being updated
- How to Iteratively Store Multiple API Calls into a useState Array
- React.NET uncaught TypeError: undefined is not a function
- fireEvent is calling Found multiple elements by: data-testid error in react-testing-library
- How to render a div element several times using map?
- Formik and validation
- material ui change the checkbox color
- React Hooks and jsx-no-lambda warning
- withRouter with connect is not working in v5.0.0?
- How to get request body to be populated