data-annotations

How can I disable a form's submit button if there are DataAnnotation validation errors?

老子叫甜甜 提交于 2019-12-14 02:30:08
问题 I've attempted binding to the change event of all inputs in the form, and checked to see if there are any validation messages on the page. The problem is that validation seems to occur after the change event, so I am checking before the errors appear on the page. I'm looking for some way to run my script after the client-side validation takes place. I'm using MVC 2 with DataAnnotation attributes on my VM. 回答1: Here's something I just hacked together that wraps the original parseElement and

Can I use my RegularExpression attribute instead of the DataAnnotations one?

时光总嘲笑我的痴心妄想 提交于 2019-12-13 21:54:34
问题 I'm trying to use a reusable regex class and use along with DataAnnotations in MVC. Something like: [RegularExpressionAttribute1(typeof(MyRegex))] This compiles but no error is thrown if the property doesn't match. It all works with the standard [RegularExpression(@"^\s*\d+(\.\d{1,2})?\s*$")] 回答1: You can create a custom validation attribute to re-use regular expressions. For email validation you would do something like this: using System.ComponentModel.DataAnnotations; public class

For data validation, where to add Html.RenderPartialAsync(“_ValidationScriptsPartial”)?

不想你离开。 提交于 2019-12-13 20:23:29
问题 In my ASP.NET Core 1.1.1 app the model validation is not working. I've noticed that some of the default views (such as login.cshtml , Register.cshtml that were created by VS2017 when the app was created) have the following code at the end. But these default views are in fact partial views. My Views are not partial views, should the following be added to end of my views as well? Or what should I be adding to the end of my views that are not partial views: @section Scripts { @{ await Html

Show Time and data using Data annotation

拜拜、爱过 提交于 2019-12-13 15:17:43
问题 I wanted to know whats is the best data type for time value only also is there a data annotation for the same ? Also I wanted to know is there a way to format datatype datetime using Data annotation to display date and time(eg: dd-mm-yy HH:MM AM/PM ) 回答1: If the object is of type DateTime then you can simply use .ToString() method for print it in required format, by passing the format string as argument to the .ToString(). Consider the following example: DateTime currentDate = DateTime.Now; /

ShortName in the Display attribute (DataAnnotations)

跟風遠走 提交于 2019-12-13 14:24:27
问题 Name attribute works proper, but ShortName doesn't work. [Display(Name = "Date of the transfer the task", ShortName = "Trans date")] public DateTime TransferDate { get; set; } Even when I delete Name attribute, ShortName is ignored ("TransferDate" displays in the column header). In the view I do this: @Html.DisplayNameFor(model => model.TransferDate) 回答1: If you look at the Description for the ShortName property on the Display Attribute you'll see that it has a pretty limited scope out of the

Creating entity relationship with renamed fields and non-primary key in primary table

◇◆丶佛笑我妖孽 提交于 2019-12-13 14:21:39
问题 The following are two partial tables in which I am trying to define a foreign key relationship. public class Form { [Key, Column("FormID")] public System.Guid FormGUID { get; set; } [Column("PatGUID")] public Nullable<System.Guid> PatientGUID { get; set; } } public class Patient { [Column("PatGUID")] public System.Guid PatientGUID { get; set; } [Key, Column("PatID")] public int PatientID { get; set; } } I've eliminated all but the relevant information, fields, navigations, etc. for this

My Own javascript validation + MicrosoftMvcValidation . Is it possible ? How

戏子无情 提交于 2019-12-13 05:40:55
问题 I would like to be able to use the MVC validation + my custom javascript validation. The MVC validation is really nice for Model Validation. The main problem here, is that I have more complexe validation. Ex: Case with MVC Validation <div class="editorSmall"> <div class="editor-label bold"> <%: Html.LabelFor(model => model.Location.CurID)%>: </div> <div class="editor-field"> <%:Html.DropDownListFor(model => model.Location.CurID, Model.CurrenciesList)%> <%: Html.ValidationMessageFor(model =>

A regular expression for using an email address as username?

ぐ巨炮叔叔 提交于 2019-12-13 05:17:49
问题 Is there a regular expression out there for using a normal username OR an email address as a username? I would like a user to be able to enter their own username, or just use their email address as the username and I am unable to find any reliable information on how to achieve this properly. It would still have to pass validation as well, for example: if the user chooses to make their own username, it would have to abide by my policy for usernames, which limits them to starting with a letter

MVC Compare not working

痞子三分冷 提交于 2019-12-13 04:49:21
问题 I'm having problems validating 2 textboxes using the [Compare-attribute. Even if they're same the client validation says they're different. In my editmodel I have: [Required] [DataType(DataType.Password)] public string Password { get; set; } [DataType(DataType.Password)] [Compare("Password", ErrorMessage = "Password not matching")] public string ConfirmPassword { get; set; } In view: <tr> <td class="editor-label"> @Html.LabelFor(model => model.User.Password): </td> <td class="editor-field">

Not getting validation Error Messages from DataAnnotation Validations

寵の児 提交于 2019-12-13 04:12:51
问题 I am have a simple model class for Login, with few DataAnnotation Validations Public Class LoginUser <Required()> _ Public Property UserName As String <Required()> _ <StringLength(8)> _ Public Property Password As String End Class View is a partial View and is as follows: <% Using (Html.BeginForm("Login", "User", FormMethod.Post))%> <% Html.EnableClientValidation()%> <%= Html.ValidationSummary() %> <table ID="loginTable" runat="server"> <tr> <td> <%= Html.LabelFor(Function(x) x.UserName)%> <