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 } = React; function App() { const [phoneNumber, setPhoneNumber] = useState(''); const handlePhoneNumberChange = (e) => { const input = e.target.value; const formattedInput = formatPhoneNumber(input); setPhoneNumber(formattedInput); }; const formatPhoneNumber = (input) => { // Remove all non-digit characters from the input const cleanedInput = input.replace(/\D/g, ''); // Apply the phone number mask if (cleanedInput.length <= 10) { return cleanedInput.replace(/(\d{3})(\d{0,3})(\d{0,4})/, '($1) $2-$3'); } else { // Handle longer phone numbers (e.g., international numbers) return cleanedInput; } }; return ( <div className='container'> <h3>React Js Input Mask for Phone Number</h3> <label className='label'>Phone Number:</label> <input type="text" className='input' value={phoneNumber} maxLength={14} onChange={handlePhoneNumberChange} placeholder="(123) 456-7890" /> </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); } .label { font-size: 16px; margin-bottom: 10px; } .input { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; outline: none; transition: border-color 0.3s; } .input::placeholder { color: #999; } .input:focus { border-color: #007bff; } </style> </body> </html>