繁体   English   中英

验证新表单 MVC

[英]Validation New Form MVC

我需要你的帮助。我在 MVC 中做一个项目,并从 Internet 下载了一个新的注册表单。 一切正常,但我想在用户插入数据时以这种形式创建错误消息(例如,错误的 email、密码等)。我为我的表单创建了新布局:

    @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>

这是我的 class: public partial class User { public int User_Id { get; 放; }

        [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; }       
    }

我必须在我的代码中插入更多内容,当客户端插入错误的 email 时,表单会显示错误消息。谢谢:!! 示例:https://learn-the-web.algonquindesign.ca/courses/web-dev-4/designing-form-errors/

尝试这个

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

或者你可以使用正则表达式

[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")]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM