Confirm Email
I want to check an email textbox when I press enter. One problem: when I press the enter button, the text disappears and nothing happens.
My code:
$(document).keypress(function(e) {
if (e.which == 13) {
mailcontrole();
}
});
function mailcontrole {
var mail = $("#email").val();
if (IsEmail(mail) == false) {
alert("false");
}
}
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!regex.test(email)) {
return false;
} else {
return true;
}
}
+3
source to share
2 answers
You have the default behavior of submitting a form with ENTER
.
$(document).keypress(function(e) {
e.preventDefault();
if (e.which == 13) {
mailcontrole();
}
});
Adding e.preventDefault();
stops this behavior!
UPDATE
Your function is missing ()
also (thanks Alex)!
function mailcontrole() {
var mail = $("#email").val();
if (IsEmail(mail) == false) {
alert("false");
}
}
+5
source to share
the form can be submitted because the form is submitted when you press the "Enter" button and which will be empty. use event.preventDefault () so it doesn't get dispatched.
try this:
$(document).keypress(function(e) {
if (e.which == 13) {
e.preventDefault();
mailcontrole();
}
});
+1
source to share