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 [email, setEmail] = useState(''); const [isValid, setIsValid] = useState(false); const [isSubmitted, setIsSubmitted] = useState(false); const handleInputChange = (e) => { setEmail(e.target.value); setIsSubmitted(false); // Reset the form submission state }; const handleSubmit = (e) => { e.preventDefault(); const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; setIsValid(regex.test(email)); setIsSubmitted(true); // Set the form submission state }; return ( <div className='container'> <h3>React Js Email Validation</h3> <form onSubmit={handleSubmit}> <input type="text" value={email} onChange={handleInputChange} placeholder="Enter your email" /> <button type="submit">Validate</button> </form> {isSubmitted && isValid && <p style={{ color: 'green' }}>Email is valid!</p>} {isSubmitted && !isValid && <p style={{ color: 'red' }}>Email is invalid!</p>} </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); width: 600px; margin: 0 auto; } input[type="text"] { padding: 10px; width: 300px; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; margin-bottom: 10px; } button[type="submit"] { padding: 10px 20px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; } button[type="submit"]:hover { background-color: #45a049; } p { font-size: 18px; margin-bottom: 10px; } </style> </body> </html>