Paul Stewart
Paul Stewart

Reputation: 53

Cant get email validation working in Jquery

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 "[email protected]" 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

Upvotes: 1

Views: 197

Answers (1)

Ryan
Ryan

Reputation: 1797

In regex.test("input.email"), you're testing if the actual string "input.email" is matching the regex. Instead, you should test the value of input.email element.

So use:

regex.test($("input.email").val())

Upvotes: 9

Related Questions