MrMistreater

Validate phone number

May 23rd, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. /// <summary>
  2. /// class for validating a phone number in our MVC Model inheriting from the ValidationAttribute. We mark it with the
  3. /// AttributeUsageAttribute <see cref="http://msdn.microsoft.com/en-us/library/tw5zxet9(v=VS.100).aspx"/> To
  4. /// tell the compiler that it can only be used on properties or fields
  5. /// </summary>
  6. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
  7. sealed public class PhoneNumberFormatAttribute : ValidationAttribute
  8. {
  9.     /// <summary>
  10.     /// our format value
  11.     /// </summary>
  12.     public string Format { get; set; }
  13.  
  14.     public PhoneNumberFormatAttribute(string format)
  15.     {
  16.         this.Format = format;
  17.     }
  18.  
  19.     /// <summary>
  20.     /// override the IsValid method of the ValidationAttribute class
  21.     /// <see cref="http://msdn.microsoft.com/en-us/library/cc679289.aspx"/>
  22.     /// </summary>
  23.     /// <param name="value">the value we're validating</param>
  24.     /// <returns></returns>
  25.     public override bool IsValid(object value)
  26.     {
  27.         var phone = (String)value;
  28.         bool validationResult = true;
  29.  
  30.         if (this.Format != null) validationResult = DoesMatchFormat(this.Format, phone);
  31.  
  32.         return validationResult;
  33.     }
  34.  
  35.     /// <summary>
  36.     /// method to check if the value provided matches our format
  37.     /// </summary>
  38.     /// <param name="format">the format we're matching too (i.e; 999-999-9999)</param>
  39.     /// <param name="value">the value we're checking</param>
  40.     /// <returns></returns>
  41.     internal bool DoesMatchFormat(string format, string value)
  42.     {
  43.         if (format.Length != value.Trim().Length) return false;
  44.  
  45.         foreach (string s in format.Split(new char[] { ',' }))
  46.         {
  47.             for (int i = 0; i < s.Length; i++)
  48.             {
  49.                 if (s[i] == 'd' && char.IsDigit(value[i]) == false)
  50.                     //return false because we're expecting a number here
  51.                     return false;
  52.  
  53.                 if (s[i] == '-' && value[i] != '-')
  54.                     //return false because we're expecting a character (such as - or ()
  55.                     return false;
  56.             }
  57.         }
  58.  
  59.         //we made it this far so it's valid
  60.         return true;
  61.     }
  62.  
  63.     /// <summary>
  64.     /// override the FormatErrorMessage method of the ValidationAttribute class
  65.     /// <see cref="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.formaterrormessage.aspx"/>
  66.     /// </summary>
  67.     /// <param name="name"></param>
  68.     /// <returns></returns>
  69.     public override string FormatErrorMessage(string name)
  70.     {
  71.         return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, this.Format);
  72.     }
  73.  
  74. }
  75.  
  76. //Sample usage
  77. [Required(ErrorMessage = "Your phone number is required")]
  78. [PhoneNumber(ErrorMessage = "A valid phone number is required")]
  79. public string PhoneNumber { get; set; }
Advertisement
Add Comment
Please, Sign In to add comment