验证新表单MVC

问题描述 投票:0回答:1

我需要你的帮助,我正在用MVC做一个项目,并从网上下载了一个新的注册表单,所有的工作都很好,但我想在这个表单中创建一个当用户插入数据时的错误信息(如错误的电子邮件,密码等)。所有的工作都很好,但我想在这个表单中创建错误信息,当用户插入e数据(例如,错误的电子邮件,密码等)。

    @model WebApp.Models.User

@{
    Layout = null;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Register</title> 
    <link rel="stylesheet" href="~/UsefullBox/fonts/material-icon/css/material-design-iconic-font.min.css">
       <link rel="stylesheet" href="~/UsefullBox/css/style.css">
</head>
<body>
    <div class="container">
        <div class="signup-content">
            <div class="signup-form">
                <h2 class="form-title">Register Form</h2>
                @using (Html.BeginForm("Register", "Home", FormMethod.Post, new { @class = "register-form", @id = "register-form" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        <label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
                        @Html.TextBoxFor(m => m.User_login, new { @placeholder = "Your Name" })<br />
                        @Html.ValidationMessageFor(m => m.User_login, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group">
                        <label for="email"><i class="zmdi zmdi-email"></i></label>
                        @Html.TextBoxFor(m => m.User_Email, new { @placeholder = "Your Email" })<br />
                        @Html.ValidationMessageFor(m => m.User_Email, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group">
                        <label for="pass"><i class="zmdi zmdi-lock"></i></label>
                        @Html.PasswordFor(m => m.User_password, new { @placeholder = "Password" })<br />
                        @Html.ValidationMessageFor(m => m.User_password, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group">
                        <label for="re-pass"><i class="zmdi zmdi-lock-outline"></i></label>
                        @Html.PasswordFor(m => m.User_password_1, new { @placeholder = "Repeat your password" })<br />
                        @Html.ValidationMessageFor(m => m.User_password_1, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group form-button">
                        <input type="submit" name="signup" id="signup" class="form-submit" value="Register" />
                    </div>  }               
            </div>
            <div class="signup-image">
                <figure><img src="~/UsefullBox/images/signup-image.jpg" alt="sing up image"></figure>
                <a href="#" class="signup-image-link">I am already member</a>
            </div>
        </div>
    </div>

</body>
</html>

这是我的类: public partial class User { public int User_Id { get; set; }。

        [Required(ErrorMessage = "Please enter the login")]
        public string User_login { get; set; }

        [Required(ErrorMessage = "Please enter the password")]
        [DataType(DataType.Password)]
        public string User_password { get; set; }

        [Required(ErrorMessage = "Reenter the password")]
        [DataType(DataType.Password)]
        [Compare("User_password")]
        public string User_password_1 { get; set; }

        [Required(ErrorMessage ="Please enter email")]
        [EmailAddress(ErrorMessage ="Invalid Email Address")]
        public string User_Email { get; set; }       
    }

我需要在我的代码中插入更多的内容,当客户端插入一个错误的电子邮件时,表单会显示错误信息。谢谢!!!!!!!!!!!!!!!!)。例子。https:/learn-the-web.algonquindesign.cacoursesweb-dev-4designing-form-errors。

c# asp.net-mvc spring-mvc
1个回答
0
投票

试试这个

[DataType(DataType.EmailAddress)]
public string User_Email { get; set; }

或者你可以使用Regex

[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")]
© www.soinside.com 2019 - 2024. All rights reserved.