Hey guys am having a bit of problems with my jQuery, I have all the input validations all working, but I cant get my REGEX code working for validating my email. tried entering “hello@mywebsite.co.uk” my personal email and my work email and all of them dont work.
Any help is highly appriciated.
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
$('.submitbutton').click(function () {
if ($("input.name").val() == '') {
alert("Name is a required field");
return false;
}
else if ($("input.email").val() == '') {
alert("Email is a required field");
return false;
}
else if (!regex.test("input.email")) {
alert("This is not a valid email address");
return false;
}
else if ($("textarea.message").val() == '') {
alert("Message is a required field");
return false;
}
else {
return true;
}
});
Cheers
In
regex.test("input.email"), you’re testing if the actual string"input.email"is matching the regex. Instead, you should test the value ofinput.emailelement.So use: