1

I am having a great deal of difficulty with getting an actual regex pattern for an email address in jQuery validate within an ASP.NET MVC view... It seems to be an issue with the "@" symbol, but using the suggested "@@" does not seem to help, either. The code looks as follows;

@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Scripts{
    <script type="text/javascript">
        $(function() {
            $('form').validate({
                rules: {
                    "Email": {
                        required: true,
                        pattern: "[A-Z0-9._%+-]+@@[A-Z0-9.-]+\.[A-Z]{2,6}$"
                    }
                },
                messages: {
                    "Email": {
                        required: "You must give an email",
                        pattern: "This is wrong."
                    }
                }
            });
        });

    </script>
}

<form>
 // html form controls, etc.
</form>

I cannot seem to get this to validate appropriately, though. If I remove the @@, then it throws an error that there has to be a code block. I've tried researching this and found very little help on the subject, which is surprising. The only reference I found was here, which didn't solve the issue: Email format validation in mvc3 view

2
  • 1
    Any particular reason you're not using the email rule/method built into the plugin? email: true Commented May 15, 2014 at 23:10
  • I have read through the documentation at least 20 times, and I did not see that. But I went to it and did a Ctrl+F search and sure enough, there is one. That worked just fine. Can you please post this as an official answer? Commented May 16, 2014 at 18:22

1 Answer 1

2

Just use the email rule/method that's included as part of the plugin.

See: http://jqueryvalidation.org/email-method/

rules: {
    Email: {  // <- name of the input field
        email: true  // <- user must enter a valid email address
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Starting to really hate the stack overflow community, as I get another down vote for a question that seems to be one without a lot of previous answers, and is an extremely valid, logical, and common everyday scenario.
Hey, I just thought you might like to know that I followed the email code and found some pretty remarkable stuff. Thanks for pointing it out. It's got some pretty awesome plumbing behind it, that helped me design an age verification plugin for jquery validate

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.