Craig
Craig

Reputation: 1794

Razor javascript function to validate email

I have the following javascript function in script tags with other javascript. It's sitting in a .net MVC3 web app in _Layout.cshtml. It YSOD's because of the '@' symbol in the regex. How can I get this to work without blowing up?

        function checkEmail(emailAddress) {
        //Match emailAddress

        var regex = /^[a-z0-9\.\_%+-]+@[a-z0-9\.\-]+\.[a-z]{2,4}$/i;

        if (emailAddress.search(regex)) {
            return false;
        }
        else {
            return true;
        }
    }

Upvotes: 0

Views: 2958

Answers (1)

Hylaean
Hylaean

Reputation: 1287

@@ escapes it

like this:

var regex = /^[a-z0-9\.\_%+-]+@@[a-z0-9\.\-]+\.[a-z]{2,4}$/i;

Upvotes: 2

Related Questions