Advertisement
NubeColectiva

Como Validar un Email en React JS

Apr 12th, 2023
1,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {useState} from 'react';
  2.  
  3. export default function App() {
  4.   const [email, setEmail] = useState('');
  5.   const [validate, setValidate] = useState(null);  
  6.  
  7.   function isValidEmail(email) {
  8.     return /\S+@\S+\.\S+/.test(email);
  9.   }
  10.  
  11.   const handleChange = event => {
  12.     if (!isValidEmail(event.target.value)) {
  13.       setValidate('Email is invalid');
  14.     } else {
  15.       setValidate('Email is valid');
  16.     }
  17.     setEmail(event.target.value);
  18.   };
  19.  
  20.   return (
  21.     <div className="App">      
  22.         <input value={email} onChange={handleChange}/>
  23.         {validate && <p style={{color: 'red'}}>{validate}</p>}          
  24.     </div>
  25.   );
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement