screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useRef } = React; function App() { const [creditCardNumber, setCreditCardNumber] = useState(''); const handleCreditCardChange = (e) => { const input = e.target.value; const sanitizedInput = input.replace(/\D/g, ''); // Remove non-numeric characters let maskedInput = ''; for (let i = 0; i < sanitizedInput.length; i++) { if (i === 4 || i === 8 || i === 12) { maskedInput += ' '; } maskedInput += sanitizedInput[i]; } setCreditCardNumber(maskedInput); }; return ( <div className='container'> <h3>React Input Mask For Credit Card</h3> <label htmlFor="creditCard" className="label-class">Credit Card Number</label> <input type="text" id="creditCard" value={creditCardNumber} onChange={handleCreditCardChange} maxLength={19} // Maximum length of a credit card number with spaces placeholder="XXXX XXXX XXXX XXXX" className="input-class" /> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { display: flex; flex-direction: column; align-items: center; margin: 0 auto; padding: 20px; width: 600px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } h3 { color: #333; } .label-class { display: block; margin: 20px 0 10px; font-weight: bold; color: #555; } .input-class { width: 100%; height: 40px; padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; outline: none; transition: border-color 0.3s; } .input-class:focus { border-color: #007bff; } /* Credit card number mask */ #creditCard { letter-spacing: .5em; } </style> </body> </html>