Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript email validation

const re = /^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;

function App() {
  const [email, setEmail] = React.useState({
    error: false,
    value: ""
  });

  const handleChange = (e: any) => {
    // Trim value & convert to lowercase
    const value = e.target.value.trim().toLowerCase();

    // Test if email is valid
    const isValidEmail = re.test(value);

    setEmail({
      value,
      error: !isValidEmail
    });
  };

  return (
    <>
      <input placeholder="Email" onChange={handleChange} />
      {email.error && <p>Please enter a valid email address.</p>}
    </>
  );
}
Source by linguinecode.com #
 
PREVIOUS NEXT
Tagged: #javascript #email #validation
ADD COMMENT
Topic
Name
7+7 =