Created
June 4, 2012 13:02
-
-
Save Dynyx/2868230 to your computer and use it in GitHub Desktop.
Phone number validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// class for validating a phone number in our MVC Model inheriting from the ValidationAttribute. We mark it with the | |
/// AttributeUsageAttribute <see cref="http://msdn.microsoft.com/en-us/library/tw5zxet9(v=VS.100).aspx"/> To | |
/// tell the compiler that it can only be used on properties or fields | |
/// </summary> | |
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | |
sealed public class PhoneNumberFormatAttribute : ValidationAttribute | |
{ | |
/// <summary> | |
/// our format value | |
/// </summary> | |
public string Format { get; set; } | |
public PhoneNumberFormatAttribute(string format) | |
{ | |
this.Format = format; | |
} | |
/// <summary> | |
/// override the IsValid method of the ValidationAttribute class | |
/// <see cref="http://msdn.microsoft.com/en-us/library/cc679289.aspx"/> | |
/// </summary> | |
/// <param name="value">the value we're validating</param> | |
/// <returns></returns> | |
public override bool IsValid(object value) | |
{ | |
var phone = (String)value; | |
bool validationResult = true; | |
if (this.Format != null) validationResult = DoesMatchFormat(this.Format, phone); | |
return validationResult; | |
} | |
/// <summary> | |
/// method to check if the value provided matches our format | |
/// </summary> | |
/// <param name="format">the format we're matching too (i.e; 999-999-9999)</param> | |
/// <param name="value">the value we're checking</param> | |
/// <returns></returns> | |
internal bool DoesMatchFormat(string format, string value) | |
{ | |
if (format.Length != value.Trim().Length) return false; | |
foreach (string s in format.Split(new char[] { ',' })) | |
{ | |
for (int i = 0; i < s.Length; i++) | |
{ | |
if (s[i] == 'd' && char.IsDigit(value[i]) == false) | |
//return false because we're expecting a number here | |
return false; | |
if (s[i] == '-' && value[i] != '-') | |
//return false because we're expecting a character (such as - or () | |
return false; | |
} | |
} | |
//we made it this far so it's valid | |
return true; | |
} | |
/// <summary> | |
/// override the FormatErrorMessage method of the ValidationAttribute class | |
/// <see cref="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.formaterrormessage.aspx"/> | |
/// </summary> | |
/// <param name="name"></param> | |
/// <returns></returns> | |
public override string FormatErrorMessage(string name) | |
{ | |
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, this.Format); | |
} | |
} | |
//Sample usage | |
[Required(ErrorMessage = "Your phone number is required")] | |
[PhoneNumber(ErrorMessage = "A valid phone number is required")] | |
public string PhoneNumber { get; set; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment