xxxxxxxxxx
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
console.log(validateEmail('my email is anystring@anystring.any')); // true
console.log(validateEmail('my email is anystring@anystring .any')); // false
xxxxxxxxxx
function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}
if( !validateEmail(emailaddress)) { /* do stuff here */ }
xxxxxxxxxx
if(value.match( /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/gi)){
//code here
}
xxxxxxxxxx
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>}
</>
);
}
xxxxxxxxxx
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation - checking email</title>
<link rel='stylesheet' href='form-style.css' type='text/css' />
</head>
<body onload='document.form1.text1.focus()'>
<div class="mail">
<h2>Input an email and Submit</h2>
<form name="form1" action="#">
<ul>
<li><input type='text' name='text1'/></li>
<li> </li>
<li class="submit"><input type="submit" name="submit" value="Submit" onclick="ValidateEmail(document.form1.text1)"/></li>
<li> </li>
</ul>
</form>
</div>
<script src="email-validation.js"></script>
</body>
</html>
xxxxxxxxxx
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/