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 OverflowUse 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
Try this...
<input type="text" class="keyup-email text-input" name="7" value="">
$(document).ready(function(){
$('.keyup-email').keyup(function() {
$('span.error-keyup-7').remove();
var inputVal = $(this).val();
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-7">Invalid Email Format.</span>');
}
});
});
fiddle:http://jsfiddle.net/091ep28h/3/
How to add another statement on keypress on email validation with jQuery
email validation in jquery
Need A code for Email Validation on keypress event
Validate email address on keyup with jQuery
Try /[^$#<>?]/ instead.
The ^ inside and at directly after the opening bracket makes the class negative.
You can learn more about this on regular-expressions.info
Or better, heres a workign simple regex that does it, using yours as a start.
http://jsfiddle.net/hG99U/1/
$('input[type=email]').on('keypress', function (e) {
var re = /[A-Z0-9a-z@\._]/.test(e.key);
if (!re) {
return false;
}
});
I like the idea of fade in/out based on valid/invalid input.
I played around a bit and following seem to be working for me ok: http://jsfiddle.net/yc9Pj/
function validateEmail(){
var validEmail = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
('#user_email').keyup(function(){
if (validEmail.test(this.value)) {
$('.masked-check').fadeIn(300)
}
else {
$('.masked-check').fadeOut(300);
//alert('Please enter a valid email');
}
});
}
validateEmail();
please note, that I adapted regex for email based on this reply: Using a regular expression to validate an email address
Moreover keyup worked best for me, as keypress didn't handle backspace (solution found here: jQuery: keyPress Backspace won't fire?)
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.
I got this working here: http://jsfiddle.net/bKT9W/2/
EDIT: I believe Peter's answer below is much better: https://stackoverflow.com/a/13298005/1415352
He suggests a better regex and also the .keyup() event which solves the backspace issue which arises when using .keypress()
You code is bit incorrect, you can do this:
$('.newsletter-signup input').first().keyup(function () {
var $email = this.value;
validateEmail($email);
});
function validateEmail(email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if (!emailReg.test(email)) {
alert('foo');
} else {
alert('bar');
}
}
In your code:
$email = $('.newsletter-signup input').first();
you are only getting the jQuery object of the first input not the value. Hence the validation is not working at all.
So, in order to get the value of the input element you can use:
var $email = this.value;
as this here refers to the $('.newsletter-signup input').first(). So need to again call it and after that get the value from it. You can directly do that using this.value.
That's a lot of code to do very little, when all you're really trying to do is this:
$('.newsletter-signup input:first').on('keyup', function(){
var valid = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(this.value);
alert(valid ? 'foo' : 'bar');
});
Also note that setting the input type to "email" does the validation for you in browsers that support HTML5.
FIDDLE
onKeyValidateis an okay name, but a better name could bevalidateKeypress.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]/?keyCharsappears to check against\x00, the null character, and\x08, the backspace character. Neither of these can ever be passed toonKeypress, so you can just take it out.The standard way to get the character code is
event.which || event.keyCode.eventis 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);".
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