Domanda

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

È stato utile?

Soluzione

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
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top