- [Required]
- [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
- [DataType(DataType.Password)]
- [Display(Name = "New password")]
- public string NewPassword { get; set; }
- [DataType(DataType.Password)]
- [Display(Name = "Confirm new password")]
- [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
- public string ConfirmPassword { get; set; }
- [RegularExpression(@"^[0-9]{0,15}$", ErrorMessage = "PhoneNumber should contain only numbers")]
- public string PhoneNumber { get; set; }
- [RegularExpression(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", ErrorMessage = "eMail is not in proper format")]
- public string eMail { get; set; }
Client Side Unobtrusive Validations
Now these attributes will take care of server side validations. To use the same from client side we need to include a couple of client side scripts and the same validation rules will work from client side too(providing immediate feedback to the user).
- <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
- [RegularExpression(@"^[A-Z]{2}$", ErrorMessage = "Country code can only be two alphabetic characters in CAPITALS")]
- public string CountryCode { get; set; }
- [Required(AllowEmptyStrings = false, ErrorMessage = "User name can't be empty")]
- public string UserName { get; set; }
- [MembershipPassword(
- MinRequiredPasswordLength = 8,
- ErrorMessage = "Your password needs to be at least 8 characters long",
- MinRequiredNonAlphanumericCharacters = 1,
- MinNonAlphanumericCharactersError = "At least one of the characters needs to be non-alphanumeric")]
- public string Password { get; set; }
- [Display]
- Helps you specify a text that can be used as label for the property’s value when editing in forms.
- public class Customer {
- [Display(Name = "Email is confirmed")]
- public bool EmailIsConfirmed { get; set; } }
- [DisplayFormat]
- This attribute is used with EditorFor and DisplayFor to specify what format to expect.
- public class Post {
- [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy HH:mm}")]
- public DateTime PostedDate { get; set; }
- // Display as currency
- [DisplayFormat(DataFormatString="{0:C}")]
- public object WritingCost;
- // Store NULL as NULL but show as empty string
- [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "")]
- public string ExtraDescription { get; set; }
- }
- [FileExtensions(Extensions = "jpg,jpeg")]
- public string Avatar { get; set; }
- [Phone]
- Verifies that value is a phone number. But what phone numbers work with this attribute?
- Only US numbers? No, the definition is quite broad and all number with or without country prefix and local prefix should work.
- The regular expression used is
- ^(\+\s?)?((?<!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$
- according to .NET Framework 4.5.1 Reference Source.
- [Phone]
- public string PhoneNumber { get; set; }
[Range(typeof(bool), "true", "true", ErrorMessage = "You must accept the terms")] public bool TermsAccepted { get; set; }
0 comments:
Post a Comment