Skip to content

Instantly share code, notes, and snippets.

@gilles-leblanc
Created October 20, 2016 02:59
Show Gist options
  • Save gilles-leblanc/652928af61ba7372508d78349ddaf484 to your computer and use it in GitHub Desktop.
Save gilles-leblanc/652928af61ba7372508d78349ddaf484 to your computer and use it in GitHub Desktop.
Telephone Number Model to match database with NUMBER(10) phone field.
public class PhoneModel
{
public long PhoneNumber { get; set; }
private string _phoneString;
[DataType(DataType.PhoneNumber)]
public string PhoneString
{
get
{
if (!string.IsNullOrWhiteSpace(_phoneString))
return _phoneString;
return DisplayPhoneNumber;
}
set
{
_phoneString = value ?? string.Empty;
if (value != null)
PhoneNumber = Convert.ToInt64(Regex.Replace(_phoneString, @"[\(\)\-\ ]", string.Empty));
}
}
public string DisplayPhoneNumber => PhoneNumber > 0 ? string.Format("{0:(###) ###-####}", PhoneNumber) : string.Empty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment