WPF's DataAnnotations annotation description

Reference: https://www.cnblogs.com/yaosuc/p/4527886.html

1. Basic verification:

using System.ComponentModel.DataAnnotations;

// Field display name 
[Display(Name = " Username " )]

// Data type (such as the type of the generated text box) 
[DataType(DataType.Password)]

// Non-null validation 
[Required(ErrorMessage = " * " )]

// Character length verification 
[StringLength( 50 , ErrorMessage = " Character length should be between 6-50 " , MinimumLength = 6 )]

// Validate Range 
[Range( 10 , 120 , ErrorMessage = " Age should be between 10-120 " )]

// Regular expression validation 
[RegularExpression( @" [A-Za-z0-9._%+-]+@[A-Za-z0-9._]+\.[A-Za-z]{2 ,4} " , ErrorMessage = "The mailbox format is incorrect " )]

// Email format verification 
[EmailAddress(ErrorMessage = "The mailbox format is incorrect " )]

// It doesn't seem to be supported under WPF.//Comparison
 verification 
[Compare( " Password " , ErrorMessage = " Inconsistent password " )]
  public  string PasswordConfirm { get ; set ; }

Among them: ErrorMessage contains custom error information . If this attribute is not added, the system default error message will be displayed (the system prompt may be blunt), and this attribute is generally added. 

     ErrorMessage allows developers to use the {0} placeholder to display the field's display name (ie [Display(Name = "Username")]). If the Display attribute is not present, the property name will be displayed. Such as:

[Required(ErrorMessage = " {0} cannot be empty! " )]
[Display(Name = "用户名")]
public string UserName { get; set; }    

 If there are other parameters in the verified feature, ErrorMessage can directly display other parameters with placeholders, such as:

[Required]
[StringLength( 100 , ErrorMessage = " Please enter {0} from {2} to {1} bits. " , MinimumLength = 6 )]
[DataType(DataType.Password)]
[Display(Name = " Password " )]
 public  string Password { get ; set ; }
 // The system will prompt: "Please enter a password of 6 to 100 digits".

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324934883&siteId=291194637