1

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

2

1 Answer 1

9

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.