user22019322
user22019322

Reputation:

Javascript validation for email address in ASP.NET MVC 5 throws a Parser Error

I have a form that uses Javascript validation in ASP.NET MVC 5. They do not want the whole form redesigned, but they want to add Javascript email validation.

I found this article:

https://www.w3resource.com/javascript/form/email-validation.php

I was excited to find something already made, so I added this Javascript to my form:

function ValidateEmail(element)
{
    var email = element.value;
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var labelValue = GetLabel(element);
    if (email.match(mailformat))
    {
        return (true)
    }
    alert("You have entered an invalid email address!")
    element.focus();
    return (false)
}

But, when I try to display the page, I get an error:

Parser Error Message: "\" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

screenshot

How do I get this Javascript to work in my ASP.NET MVC 5 form?

Upvotes: 0

Views: 68

Answers (1)

function ValidateEmail(element) {
    var email = element.value;
    var mailformat = /^\w+([\.-]?\w+)*@@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (email.match(mailformat)) {
        return true;
    }
    alert("You have entered an invalid email address!");
    element.focus();
    return false;
}

Try this, I have escaped the single @, there can be parsing issues with it.

Upvotes: 1

Related Questions