关键词:
【中文标题】仅在电子邮件无效时显示错误消息【英文标题】:only show the errormessage when email is invalid 【发布时间】:2012-04-23 14:11:29 【问题描述】:我在 ASP MVC3 中工作。我有一个输入客户联系信息的模型。 该信息是必需的。当 viewstat 无效时,文本框必须有一个红色边框(这有效)en no errormessage。但是当 emailinput 无效时,错误必须在视图上可见。因此,只有电子邮件注释的错误消息必须可见,而不是必需的注释。我不知道该怎么做。
型号:
[Required]
public string FirstName get; set;
[Required]
public string Name get; set;
[Required]
public string Address get; set;
[Required]
public string PostalCode get; set;
[Required]
public string City get; set;
[Required]
public string Country get; set;
[Required]
public string PhoneNumber get; set;
public string Fax get; set;
[Required]
[Email]
public string Email get; set;
查看:
<div class="row-fluid">
<div class="span6">
<div data-role="fieldcontain" class="ui-hide-label">
<label for=@Lingo.language.Obj_Telefoon>@Lingo.language.Obj_Telefoon</label>
<span>@Html.TextBoxFor(m => m.PhoneNumber, new style = "width:90%!important;", placeholder = @Lingo.language.Obj_Telefoon )</span>
</div>
</div>
<div class="span6">
<div data-role="fieldcontain" class="ui-hide-label">
<label for=@Lingo.language.Obj_Fax>@Lingo.language.Obj_Fax</label>
<span>@Html.TextBoxFor(m => m.Fax, new placeholder = @Lingo.language.Obj_Fax )</span>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div data-role="fieldcontain" class="ui-hide-label">
<label for=@Lingo.language.Obj_Email>@Lingo.language.Obj_Email</label>
<span>@Html.TextBoxFor(m => m.Email, new style = "width:98%!important;", placeholder = @Lingo.language.Obj_Email )</span>
<span>@Html.ValidationMessageFor(m => m.Email)</span>
</div>
</div>
</div>
【问题讨论】:
【参考方案1】:写下所需的信息,如下所示:
[Required( ErrorMessage = "The first name is required" )]
public string FirstName get; set;
【讨论】:
【参考方案2】:如果您希望它是必需的,但不想显示“必需”错误消息,您可以将消息设置为单个空格(以避免运行时错误“ErrorMessageString 或 ErrorMessageResourceName 必须是设置,但不能同时设置。”)
[Required(ErrorMessage = " ")]
[Email(ErrorMessage = "Must be a valid email address.")]
public string Email get; set;
如果您还没有 Email
属性,您可以创建一个类文件(例如 EmailAttribute.cs),其中包含:
using System.ComponentModel.DataAnnotations;
public class EmailAttribute: RegularExpressionAttribute
public EmailAttribute()
: base(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]2,4$")
【讨论】:
[Required(ErrorMessage = "")] 给出一个错误,但是当你为错误放置一个空格时它会起作用。 ==> [必需(ErrorMessage = " ")] 谢谢:) 很遗憾,使用正则表达式验证电子邮件地址可能会返回错误结果。考虑这些(通过您发布的正则表达式的错误地址):double..dots..are..not..allowed@example.com
、me@hello.-.com
,甚至是me@..com
- 对于一个完整的、符合 IETF 的电子邮件验证,我建议使用我们很酷的 email validation component 作为后端对于您的 EmailAttribute 类。 ;)Python/Django:如何在无效登录时显示错误消息?
】Python/Django:如何在无效登录时显示错误消息?【英文标题】:Python/Django:Howtodisplayerrormessagesoninvalidlogin?【发布时间】:2018-06-0401:42:43【问题描述】:我正在尝试为我的Django(2.0)网站进行登录,到目前为止,我已经为现有帐户进... 查看详情
为啥使用 Oracle_cx 时显示“无效长度”错误消息?
】为啥使用Oracle_cx时显示“无效长度”错误消息?【英文标题】:Whyis\'invalidlength\'errormessageshowingwhenusingOracle_cx?为什么使用Oracle_cx时显示“无效长度”错误消息?【发布时间】:2019-07-2315:25:36【问题描述】:使用cx_Oracle,我正在... 查看详情
BootstrapVue VeeValidate - 当表单无效时显示额外的错误消息
】BootstrapVueVeeValidate-当表单无效时显示额外的错误消息【英文标题】:BootstrapVueVeeValidate-showanextraerrormessagewhenformisinvalid【发布时间】:2020-04-1616:12:24【问题描述】:我在我的Laravel+Vue.jsSPA(单页应用程序)中使用BootstrapVue和VeeVal... 查看详情
日期选择器仅在聚焦时显示错误
】日期选择器仅在聚焦时显示错误【英文标题】:Datepickershowingerroronlyonfocusout【发布时间】:2022-01-2221:35:14【问题描述】:我正在使用材料日期选择器:https://material.angular.io/components/datepicker/overview。我有一个异步验证器,它调... 查看详情
无效登录/注册时创建错误消息
】无效登录/注册时创建错误消息【英文标题】:Creatinganerrormessagewheninvalidlogin/signup【发布时间】:2021-12-0805:26:19【问题描述】:我想在用户登录无效(密码错误等)时显示错误消息我目前的想法是我想在变量$login_errorisset时显示... 查看详情
如何在登录失败时显示错误消息?
...:当我尝试使用错误的凭据时,我无法显示如下消息:“无效的用户名/密码”。我检查了Auth文件夹中的所有控制器,但没有 查看详情
如何仅在鼠标悬停时显示文本
】如何仅在鼠标悬停时显示文本【英文标题】:Howcandisplaytextonlywhenmouseover【发布时间】:2016-05-1707:25:47【问题描述】:我有这个样本:link编码HTML:SED潜伏期Sedutperspiciatisundeomnisistenatus错误坐voluptatemaccusantiumdoloremquelaudantium,totamre... 查看详情
如何停止 ValidationMessageFor 在加载时显示错误消息?
】如何停止ValidationMessageFor在加载时显示错误消息?【英文标题】:HowdoyoustopValidationMessageFordisplayinganerrormessageonload?【发布时间】:2011-11-0400:36:59【问题描述】:我已向视图添加了一条验证消息“CountryId自定义错误”。问题是它... 查看详情
Flutter 项目运行时显示错误无效参数:源不得为空
】Flutter项目运行时显示错误无效参数:源不得为空【英文标题】:FlutterprojectwhenrunshowingErrorInvalidargument(s):Thesourcemustnotbenull【发布时间】:2018-12-3013:56:37【问题描述】:我是新来的颤振。我在尝试运行时创建了一个简单的颤振项... 查看详情
Paypal在上传购物车时显示无效数量错误
】Paypal在上传购物车时显示无效数量错误【英文标题】:Paypalshowinginvalidquantityerrorwhenuploadingshoppingcart【发布时间】:2013-04-0521:14:05【问题描述】:我已经制作了一个贝宝表格,将我的自定义购物车上传到贝宝,以便客户可以使... 查看详情
表单验证错误消息以角度提交时显示
】表单验证错误消息以角度提交时显示【英文标题】:Formvalidationerrormessageshowonsubmitinangular【发布时间】:2019-02-2400:43:19【问题描述】:我正在尝试以Angular6验证表单,但无法正常工作。我的代码在stackblitz中。如何验证该表单以... 查看详情
microsoft edge 在访问私有应用程序时显示错误证书无效
】microsoftedge在访问私有应用程序时显示错误证书无效【英文标题】:microsoftedgeshowserrorcertinvalidonaccessingprivateapplications【发布时间】:2020-12-0818:22:29【问题描述】:在Mac上下载Edge版本84.0.522.59,同时打开私人网站时出现错误证书... 查看详情
在 dropzone 文件上传时显示成功/错误消息
】在dropzone文件上传时显示成功/错误消息【英文标题】:Showsuccess/errormessageondropzonefileupload【发布时间】:2016-11-2817:48:17【问题描述】:我正在使用dropzone文件上传,当我从控制器返回成功/错误时,如何在我的视图文件中显示它.... 查看详情
直接在浏览器中点击 post 方法 url 时显示错误消息
】直接在浏览器中点击post方法url时显示错误消息【英文标题】:Showerrormessagewhilehittingpostmethodurldirectlyinbrowser【发布时间】:2019-07-2421:20:22【问题描述】:我想在使用javaspringboot直接点击post方法url时显示错误消息,所以我使用了t... 查看详情
选择选项时显示未选择选项的表单错误消息
】选择选项时显示未选择选项的表单错误消息【英文标题】:Formshowingnooptionisselectederrormessagewhenoptionsareselected【发布时间】:2016-08-1723:14:01【问题描述】:我有以下表格:<formaction="#"method="POST"enctype="multipart/form-data">from:<se... 查看详情
如何仅在使用联系人选择器存在电话号码时显示
】如何仅在使用联系人选择器存在电话号码时显示【英文标题】:HowtoshowOnlyifphoneNumberispresentusingcontactpicker【发布时间】:2016-10-2408:21:51【问题描述】:我正在使用联系人选择器库来选择多个联系人,但如果一个联系人不包含任... 查看详情
从按钮操作中调用变量并在出现错误时显示错误消息
】从按钮操作中调用变量并在出现错误时显示错误消息【英文标题】:Callavariablefromabuttonaction&displayanerrormessageincaseofone【发布时间】:2022-01-2103:06:36【问题描述】:编辑:当我点击GUI的开始按钮时,我希望执行他部分中的代... 查看详情
当 Apollo GraphQL 失败并出现错误时显示 MatSnackBar 消息
】当ApolloGraphQL失败并出现错误时显示MatSnackBar消息【英文标题】:showingMatSnackBarmessagewhenApolloGraphQLfailswithanerror【发布时间】:2020-12-2221:52:34【问题描述】:我有一个使用Angular10和ApolloGraphQL的网站。每当请求失败时,我想向使用M... 查看详情