关于登录系统邮箱验证误将.info域名标记为无效的技术求助
Hey there! I spot the problem immediately—your email validation regex is restricting top-level domains (TLDs) to only 2-3 characters, which is why .info (a 4-character TLD) is getting incorrectly marked as invalid. Let's break this down and fix it step by step.
The Root Cause
Your validation regex includes this segment:
(\.\w{2,3})+
The {2,3} quantifier limits the length of the TLD to 2 or 3 characters. But modern TLDs like .info, .club, and .online are 4+ characters long, so they fail to match this pattern.
Fixed Validation Code
Update the regex to allow longer TLDs (I’ve set it to up to 6 characters, which covers most common TLDs including newer ones):
$('#email').keyup(function () { if ($("#email").val().match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/)) { $("#emailmessage").html('Email is valid!'); } else { $("#emailmessage").html('This is invalid email!'); } });
If you want full compliance with ICANN rules (which allow TLDs up to 63 characters), you can adjust the quantifier to {2,63} instead:
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,63})+$/
Bonus Fix: Onkeypress Event Syntax Error
Your onkeypress attribute has a syntax bug—the comma operator you’re using doesn’t actually enforce the space-blocking logic. Update it to properly check for space key presses:
<input type="email" placeholder="E-mail..." id="email" name="email" onkeypress="return (event.keyCode !== 32 && event.which !== 32);" onpaste="return false;" > <div class="message" id="emailmessage"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
Quick Notes
- The
type="email"attribute already provides basic browser-level validation, so make sure your custom regex aligns with that behavior. - Disabling paste might frustrate some users (e.g., those who want to paste a saved email address), but that’s a call you can make based on your specific use case.
内容的提问来源于stack exchange,提问作者popov

