My team keeps coding repeated validators so I posted some of the validators that we usually use so that they could simply "cut-n-paste" these items, reduce time, and avoid DRY.
public class MyValidator
{
[Required(ErrorMessage = "Last name is required.")]
[StringLength(50, ErrorMessage = "Must be less than 50 characters")]
public string LastName { get; set; }
[Required(ErrorMessage = "First name is required.")]
[StringLength(50, ErrorMessage = "Must be less than 50 characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Telephone No. is required.")]
[Range(100, 999999999999, ErrorMessage="Invalid Phone No.")]
public int Telephone { get; set; }
[Required(ErrorMessage = "Salary is required.")]
[Range(0, 999999999999, ErrorMessage = "Invalid amount.")]
public decimal Salary { get; set; }
[Required(ErrorMessage = "E-mail is required.")]
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Invalid e-mail")]
public string Email {get; set;}
}
No comments:
Post a Comment