Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Last active June 6, 2025 09:47
Show Gist options
  • Save tranchausky/5a2264d024a2e5bb2fa52ac8b4ac185f to your computer and use it in GitHub Desktop.
Save tranchausky/5a2264d024a2e5bb2fa52ac8b4ac185f to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Email Validation Strict Demo</title>
<style>
input {
width: 320px;
padding: 8px;
font-size: 16px;
}
.valid {
border: 2px solid green;
}
.invalid {
border: 2px solid red;
}
#message {
font-size: 14px;
margin-top: 5px;
}
#message.valid {
color: green;
}
#message.invalid {
color: red;
}
</style>
</head>
<body>
<h2>Strict Email Validation</h2>
<input type="text" id="emailInput" placeholder="Enter your email" />
<div id="message"></div>
<script>
function isValidEmail(email) {
const emailRegex = /^[a-zA-Z0-9](\.?[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-])*[a-zA-Z0-9]@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/;
if (email.includes('..')) return false;
if (email.indexOf('#') !== -1) return false;
return emailRegex.test(email);
}
const input = document.getElementById('emailInput');
const message = document.getElementById('message');
input.addEventListener('blur', () => {
const val = input.value.trim();
if (!val) {
input.className = '';
message.textContent = '';
return;
}
if (isValidEmail(val)) {
input.className = 'valid';
message.textContent = 'Valid email address';
message.className = 'valid';
} else {
input.className = 'invalid';
message.textContent = 'Invalid email address';
message.className = 'invalid';
}
});
</script>
</body>
</html>
$.validator.methods.emailnew = function (email, element) {
const emailRegex = /^[a-zA-Z0-9](\.?[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-])*[a-zA-Z0-9]@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/;
if (email.includes('..')) return false;
if (email.indexOf('#') !== -1) return false;
return emailRegex.test(email);
}
console.log(isValidEmail("[email protected]")); // false (TLD 1 char)
console.log(isValidEmail("abc.def@mail#archive.com"));// false (# in domain)
console.log(isValidEmail("abc.def@mail")); // false (no TLD)
console.log(isValidEmail("[email protected]")); // false (double dot domain)
console.log(isValidEmail("[email protected]")); // false (local ends with '-')
console.log(isValidEmail("[email protected]")); // false (double dot local)
console.log(isValidEmail("[email protected]")); // false (local starts with '.')
console.log(isValidEmail("abc#[email protected]")); // false (# in local)
console.log(isValidEmail("[email protected]")); // true (valid)
console.log(isValidEmail("[email protected]")); // true (valid)
gfdgggggggggggggggggggggggggggggggggrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyywwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwffffffffffffffffffffffffff@
function isValidEmail(email) {
// Basic regex with stricter local and domain rules:
// Local part: starts and ends with alphanumeric, no consecutive dots or special chars at ends
// Domain: labels with letters, digits, hyphens, separated by single dots, TLD >= 2 letters
const emailRegex = /^[a-zA-Z0-9](\.?[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-])*[a-zA-Z0-9]@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/;
// Disallow consecutive dots anywhere
if (email.includes('..')) return false;
// Disallow invalid characters (only allow listed in regex)
// Extra check for # in email
if (email.indexOf('#') !== -1) return false;
// Validate with regex
return emailRegex.test(email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment