Sunday, January 11, 2015

DataAnnotations


  1.  
  1. [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]


  1. [Required]
  2.         [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
  3.         [DataType(DataType.Password)]
  4.         [Display(Name = "New password")]
  5.         public string NewPassword { get; set; }
  6.  
  7.         [DataType(DataType.Password)]
  8.         [Display(Name = "Confirm new password")]
  9.         [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
  10.         public string ConfirmPassword { get; set; }


  1. [RegularExpression(@"^[0-9]{0,15}$", ErrorMessage = "PhoneNumber should contain only numbers")]
  2.         public string PhoneNumber { get; set; }
  3.  
  4.         [RegularExpression(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", ErrorMessage = "eMail is not in proper format")]
  5.         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).

  1. <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
  2. <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>



  1. [RegularExpression(@"^[A-Z]{2}$", ErrorMessage = "Country code can only be two alphabetic characters in CAPITALS")]
  2.       public string CountryCode { get; set; }



  1. [Required(AllowEmptyStrings = false, ErrorMessage = "User name can't be empty")]
  2.       public string UserName { get; set; }
  3.  
  4.       [MembershipPassword(
  5.              MinRequiredPasswordLength = 8,
  6.              ErrorMessage = "Your password needs to be at least 8 characters long",
  7.              MinRequiredNonAlphanumericCharacters = 1,
  8.              MinNonAlphanumericCharactersError = "At least one of the characters needs to be non-alphanumeric")]
  9.       public string Password { get; set; }




  1. [Display]
  2. Helps you specify a text that can be used as label for the property’s value when editing in forms.
  3.  
  4. public class Customer {
  5.     [Display(Name = "Email is confirmed")]
  6.     public bool EmailIsConfirmed { get; set; } }
  7.     
  8.  
  9. [DisplayFormat]
  10. This attribute is used with EditorFor and DisplayFor to specify what format to expect.
  11.   
  12.     public class Post {
  13.  
  14.      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy HH:mm}")]
  15.      public DateTime PostedDate { get; set; }
  16.     // Display as currency
  17.      [DisplayFormat(DataFormatString="{0:C}")]
  18.      public object WritingCost;
  19.         
  20.      // Store NULL as NULL but show as empty string
  21.      [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "")]
  22.      public string ExtraDescription { get; set; }
  23.     
  24.     }
  25.   



  1. [FileExtensions(Extensions = "jpg,jpeg")]
  2. public string Avatar { get; set; }
  3.  
  4. [Phone]
  5. Verifies that value is a phone number. But what phone numbers work with this attribute?
  6. Only US numbers? No, the definition is quite broad and all number with or without country prefix and local prefix should work.
  7. The regular expression used is
  8. ^(\+\s?)?((?<!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$
  9. according to .NET Framework 4.5.1 Reference Source.
  10.  
  11. [Phone]
  12. public string PhoneNumber { get; set; }

 [Range(typeof(bool), "true""true", ErrorMessage = "You must accept the terms")]
 public bool TermsAccepted { getset; }

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More