1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System.ComponentModel.DataAnnotations; [Required] [Display(Name = "Release Date")] [Required(ErrorMessage = "The Encaissment ID must be submitted")] [StringLength(10)] [Range(1, 100)] [StringLength(5)] [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 = "Email is not valid")] [DataType(DataType.EmailAddress)] [DataType(DataType.PhoneNumber)] [DataType(DataType.Url)] [DisplayFormat(DataFormatString = "{0:d}")] [DataType(DataType.Currency)] @Html.ValidationMessageFor(model => model.Price) [StringLength(8, ErrorMessage = "Name length can't be more than 8.")] [StringLength(8, ErrorMessage = "{0} length must be between {2} and {1}.", MinimumLength = 6)] @Html.ValidationSummary(true) |
[CreditCard]: Validates that the property has a credit card format. Requires jQuery Validation Additional Methods.
[Compare]: Validates that two properties in a model match.
[EmailAddress]: Validates that the property has an email format.
[Phone]: Validates that the property has a telephone number format.
[Range]: Validates that the property value falls within a specified range.
[RegularExpression]: Validates that the property value matches a specified regular expression.
[Required]: Validates that the field is not null. See [Required] attribute for details about this attribute’s behavior.
[StringLength]: Validates that a string property value doesn’t exceed a specified length limit.
[Url]: Validates that the property has a URL format.
[Remote]: Validates input on the client by calling an action method on the server. See [Remote] attribute for details about this attribute’s behavior.
1 2 3 4 5 6 7 8 9 10 11 12 |
[AcceptVerbs("GET", "POST")] public IActionResult VerifyEmail(string email) { if (!_userService.VerifyEmail(email)) { return Json($"Email {email} is already in use."); } return Json(true); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
[AcceptVerbs("GET", "POST")] public IActionResult VerifyName(string firstName, string lastName) { if (!_userService.VerifyName(firstName, lastName)) { return Json($"A user named {firstName} {lastName} already exists."); } return Json(true); } |
1 2 3 4 5 6 7 8 9 |
[Remote(action: "VerifyName", controller: "Users", AdditionalFields = nameof(LastName))] [Display(Name = "First Name")] public string FirstName { get; set; } [Remote(action: "VerifyName", controller: "Users", AdditionalFields = nameof(FirstName))] [Display(Name = "Last Name")] public string LastName { get; set; } |
참조 : https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0