ASP。NET MVC客户端电子邮件验证不起作用

时间:2022-01-31 15:10:36

In my registration form I want to put some client-side validations with MVC regular expressions, but it doesn't work... Here is part of model's code

在我的注册表单中,我想使用MVC正则表达式进行一些客户端验证,但是它不起作用……下面是model的部分代码

        [Required]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address*")]
        public string Email { get; set; }

But the message don't want to appear anyway... What is wrong here?

但无论如何,这条信息都不想出现……什么是错误的吗?

EDIT

编辑

In my View I have this

在我看来,我有这个

            <div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Email)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Email)
                @Html.ValidationMessageFor(m => m.Email)
            </div>
            </div>

And I've also include jQuery libraries in my layout

我还在我的布局中包含了jQuery库。

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/Scripts/jquery.validate.min.js"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    @RenderSection("Head")
</head>

2 个解决方案

#1


27  

I've finally figured it out... Replacing Required attribute after RegularExpression attribute solved the problem)))

我终于明白了……在正则表达式属性解决了问题后替换所需的属性)

Instead this

而不是这个

        [Required]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]

        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address*")]
        public string Email { get; set; }

now I have

现在我有

        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
        [Required]

        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address*")]
        public string Email { get; set; }

And it now works perfectly!

现在它工作得很完美!

#2


3  

You also need

你还需要

1.@Html.ValidationMessageFor(m => m.Email) in the view where you want the message to appear

1 .@Html。在希望消息出现的视图中显示ValidationMessageFor(m => m. email)

2.Include the JavaScript files required for client side validation. If you are using unobtrusive validation then this will be

2。包括客户端验证所需的JavaScript文件。如果您正在使用不引人注目的验证,那么这将是

jQuery, 
jQuery validate, 
jQuery validate unobtrusive 

in that order.

这个顺序。

#1


27  

I've finally figured it out... Replacing Required attribute after RegularExpression attribute solved the problem)))

我终于明白了……在正则表达式属性解决了问题后替换所需的属性)

Instead this

而不是这个

        [Required]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]

        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address*")]
        public string Email { get; set; }

now I have

现在我有

        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
        [Required]

        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address*")]
        public string Email { get; set; }

And it now works perfectly!

现在它工作得很完美!

#2


3  

You also need

你还需要

1.@Html.ValidationMessageFor(m => m.Email) in the view where you want the message to appear

1 .@Html。在希望消息出现的视图中显示ValidationMessageFor(m => m. email)

2.Include the JavaScript files required for client side validation. If you are using unobtrusive validation then this will be

2。包括客户端验证所需的JavaScript文件。如果您正在使用不引人注目的验证,那么这将是

jQuery, 
jQuery validate, 
jQuery validate unobtrusive 

in that order.

这个顺序。