c# - 验证新表单 MVC
问题描述
我需要你的帮助。我在 MVC 中做一个项目,并从 Internet 下载了一个新的注册表单。一切正常,但我想在用户插入数据时以这种形式创建错误消息(例如错误的电子邮件、密码等)。我为我的表单创建了新布局:
@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; 放; }
[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.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")]
推荐阅读
- javascript - 在反应应用程序上存储资产/内容的最佳方式是什么?
- php - 我的弹出窗口只显示 mySQL 数据库中第一行的信息
- xml - 如何扩展主题样式属性?
- python - python3中的CSV到JSON,具有重复值
- javascript - 无法使用 Vue.js 和 Axios 上传文件
- python - Numpy追加到空数组
- javascript - 切换img src时添加transition
- html - 如何获取 HTML 页面上某个部分的文本
- heroku - 从托管在 cedar-14 堆栈上的 heroku 应用程序转发?
- angular - 如何以 Angular 形式获取下拉选项的值