Use keypress event. Try with this -

onkeypress="alert(/([A-Z0-9a-z_-][^@])+?@[^$#<>?]+?\.[\w]{2,4}/.test(this.value))"

I would prefer to use onblur in this case.

You can also use jquery -

HTML

<input id="email_address">
<span id="error" style="display:none;color:red;">Wrong email</span>

Jquery

$('#email_address').on('keypress', function() {
    var re = /([A-Z0-9a-z_-][^@])+?@[^$#<>?]+?\.[\w]{2,4}/.test(this.value);
    if(!re) {
        $('#error').show();
    } else {
        $('#error').hide();
    }
})

DEMO

Answer from Sougata Bose on Stack Overflow
🌐
ASPSnippets
aspsnippets.com › Articles › Perform-Email-validation-using-OnKeyPress-in-JavaScript.aspx
Perform Email validation using OnKeyPress in JavaScript
January 28, 2019 - <input type="text" id="txtEmail" onkeyup="ValidateEmail();" /> ... OnKeyUp event handler in JavaScript and if the Email Address is invalid, the error message will be displayed next to the TextBox.
Discussions

How to add another statement on keypress on email validation with jQuery
I believe what you're trying to achieve is for the email validation to run after each key-stroke and to keep notifying the user if their email was valid or not until it is valid. There is a jQuery .keypress() event to identify a keypress event. More on stackoverflow.com
🌐 stackoverflow.com
email validation in jquery
Ask questions here if you're new to jQuery or JavaScript and need help making sense of it all · Ask questions and report issues related to using jQuery. Discuss anything related to jQuery itself. For issues with plugins, ask in the jQuery Plugins forum · Want to discuss the forum itself? More on forum.jquery.com
🌐 forum.jquery.com
Need A code for Email Validation on keypress event
Hello All, I need a code for email validation on keypress event.If improper email id is entered a messagebox must have to appear showing a perticular message.I tried the as shown in the following ... More on c-sharpcorner.com
🌐 c-sharpcorner.com
Validate email address on keyup with jQuery
Im trying to validate an email address onkeyup only the following doesn't ever seem to return valid, Can anybody see where i may be going wrong? I only have 1 input and my .first() selector is tar... More on stackoverflow.com
🌐 stackoverflow.com
🌐
JSFiddle
jsfiddle.net › gabrieleromanato › Ra85j
jQuery: inline email validation - JSFiddle - Code Playground
They will eventually disappear (since they're anonymous), but now you'll have time to save them under your user after logging in. Added Tailwind CSS to the list of precompilers - even though it's really HTML, you can activate it from the dropdown in the CSS panel. Tailwind CSS would be best used with HTML Hot-reload enabled. We've replaced the dated code linter and formatted with more modern ones: Stylelint and Prettier.
🌐
CodeSpeedy
codespeedy.com › home › live email validation using jquery
Live email validation using jQuery - CodeSpeedy
July 31, 2018 - The above jQuery code will check the email if it is a valid email or not every time the keyup event occurs. If the email is not a valid email then it will show a text in red like “myemail is not a valid email”. This text appearing on the ...
🌐
JavaScript
js.webjeda.com › js-email-validation
Email validation using Javascript - JavaScript
October 26, 2016 - <html> <body> <div style="padding:40px; border: 1px dashed #777;margin: 1em;"> <span id="check">Enter email</span><br /> <input type="text" name="email" onkeydown="validate(this.value);"> </div> <script language="JavaScript"> var ev = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/; var x = document.getElementById("check"); function validate(email){ if(!ev.test(email)) { x.innerHTML = "Not a valid email"; x.style.color = "red" } else { x.innerHTML = "Looks good!"; x.style.color = "green" } } </script> </body> </html>
🌐
Jquery
forum.jquery.com › topic › email-validation-in-jquery
email validation in jquery
Ask questions here if you're new to jQuery or JavaScript and need help making sense of it all · Ask questions and report issues related to using jQuery. Discuss anything related to jQuery itself. For issues with plugins, ask in the jQuery Plugins forum · Want to discuss the forum itself?
Find elsewhere
🌐
C# Corner
c-sharpcorner.com › forums › need-a-code-for-email-validation-on-keypress-event
Need A code for Email Validation on keypress event
if(i1==-1) { alert(" E-mail address must have a '@' "); document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus(); return false; } if(i3==-1) { alert(" E-mail address must have a '.' "); document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus(); return false; }
🌐
Agyan Adda
agyanadda.com › question › how-to-validate-name-using-regex-onkeypress-in-javascript
How to validate name using regex onkeypress in JavaScript - agyanadda
Alphabets and Numbers. var regex = /^[A-Za-z0-9]+$/; //Validate TextBox value against the Regex. var isValid = regex.test(String.fromCharCode(keyCode)); if (!isValid) { lblError.innerHTML = "Only Alphabets & Numbers allowed."; } return isValid; } </script> <hr> <p>Note: No special character ...
🌐
Stack Overflow
stackoverflow.com › questions › 32624667 › how-to-validate-an-email-filed-on-key-press
jquery - How to validate an email filed on key press - Stack Overflow
This field i want to validate with email validation ,how to do that,please somebody help ,Thank in advance ... $('#youremailfield').on('keypress', function() { var re = /([A-Z0-9a-z_-][^@])+?@[^$#<>?]+?\.[\w]{2,4}/.test(this.value); if(!re) { $(".errmsg").html("Wrong Email").show().fadeOut(3000); } else { $(".errmsg").hide(); } });
Top answer
1 of 2
6
  • onKeyValidate is an okay name, but a better name could be validateKeypress.

  • It seems very silly to store a RegExp as a string, and then construct it every time. Why not just declare var alpha = /[ A-Za-z]/?

  • keyChars appears to check against \x00, the null character, and \x08, the backspace character. Neither of these can ever be passed to onKeypress, so you can just take it out.

  • The standard way to get the character code is event.which || event.keyCode.

  • event is a global; I don't think you need to pass it in.

Here's a proposed rewrite:

var alpha = /[ A-Za-z]/;
var numeric = /[0-9]/; 
var alphanumeric = /[ A-Za-z0-9]/;

function validateKeypress(validChars) {
    var keyChar = String.fromCharCode(event.which || event.keyCode);
    return validChars.test(keyChar) ? keyChar : false;
}

The HTML will have to change to onkeypress="validateKeypress(alpha);".

2 of 2
1

The thing that I was able to pick out, and it's more of a nitpick type of things is that you should turn your last if statement around

if (!validChars.test(keychar) && !keyChars.test(keychar))   {
    return false
} else{
    return keychar;
}

should look like this

if (validChars.test(keychar) && keyChars.test(keychar)) {
    return keychar;
} else {
    return false;
}

Do your Positive first. most people like this better than all the negatives.

Side Note: for code golfing you just shaved 2 characters as well as made it more standard compliant if this nitpick can be considered a standard.

Short Version:

If you know Ternary operators and would like to use them instead of this simple if statement, @renatargh mentioned that you could make this super short

return validChars.test(keychar) && keyChars.test(keychar) ? keychar : false;

Also, var alphanumeric = "[ A-Za-z0-9]"; is never used (in this code block) and neither is var keyChars = /[\x00\x08]/;

you should just get rid of them

🌐
OutSystems
outsystems.com › forums › discussion › 92866 › email-validation-on-keypress
Email Validation on keypress | OutSystems
I need to validate the email from the client side. So, I have configured validation, and it is working. but now I want to restrict the user from entering special characters other than the required format · I have implement the JavaScript which restrict the special characters and only allow ...
🌐
EmailListValidation
emaillistvalidation.com › blog › mastering-email-validation-on-keyup-with-jquery-a-comprehensive-expert-guide
Mastering Email Validation on Keyup with jQuery: A Comprehensive Expert Guide
October 6, 2023 - However, server-side validation remains essential for security and data integrity. In conclusion, jQuery keyup email validation is a valuable technique to enhance user experience and data accuracy in web forms.
🌐
EmailListValidation
emaillistvalidation.com › blog › validate-email-on-keyup-with-jquery-enhance-user-experience-and-data-accuracy
Validate Email on Keyup with jQuery: Enhance User Experience and Data Accuracy
August 16, 2023 - Here's an example of using the jQuery Validation plugin to validate an email input: ... In this example, the keyup event is used to trigger the validation process. The validate method sets up the validation rules for the email input, including ...
🌐
Stack Overflow
stackoverflow.com › questions › 21823199 › validate-email-field-on-keyup-event-in-jquery-validate-engine
Validate email field on keyup event in jquery validate Engine
May 24, 2017 - (function($){ $.fn.validationEngineLanguage = function(){ }; $.validationEngineLanguage = { newLang: function(){ $.validationEngineLanguage.allRules = { "required": { // Add your regex rules here, you can take telephone as an example "regex": "none", "alertText": "* Dieses Feld ist erforderlich", "alertTextCheckboxMultiple": "* Please select an option", "alertTextCheckboxe": "* This checkbox is required", "alertTextDateRange": "* Both date range fields are required" }, "Marke": { "regex": /^\s*[0-9a-zA-Z][0-9a-zA-Z ]*$/ || "none", "alertText": "* Bitte Marke angeben" }, "requiredInFunction": {