public PhoneFormatter(PhoneFormat format) { Format = format; switch (Format) { case PhoneFormat.RussiaOnlyHyphenated: MaxStringLength = 16; Starttext = "+7"; SeparatorPositions = new SeparatorPosition[] { new SeparatorPosition(2, "-"), new SeparatorPosition(6, "-"), new SeparatorPosition(10, "-"), new SeparatorPosition(13, "-") }; break; case PhoneFormat.RussiaOnlyShort: MaxStringLength = 12; Starttext = "+7"; SeparatorPositions = new SeparatorPosition[] { }; break; case PhoneFormat.BracketWithWhitespaceLastTen: MaxStringLength = 19; Starttext = ""; SeparatorPositions = new SeparatorPosition[] { new SeparatorPosition(0, "("), new SeparatorPosition(4, ") "), new SeparatorPosition(9, " - "), new SeparatorPosition(14, " - ") }; break; case PhoneFormat.DigitsTen: MaxStringLength = 10; Starttext = ""; SeparatorPositions = new SeparatorPosition[] { new SeparatorPosition(0, "") }; break; } }
public static string ToPhoneFormat(this string value, PhoneFormat phoneFormat = PhoneFormat.Parentheses, string extPrefix = " x") { // only numbers value = value.Clean(StringContent.NumbersOnly); if (value.IsNullOrEmpty()) { return(value); } if (value.StartsWith("1")) { value = value.Substring(1, value.Length - 1); } // break out string phone = string.Empty; string ext = string.Empty; if (value.Length < 10) { // dont know what this is return(value); } else if (value.Length == 10) { phone = value; } else if (value.Length > 10) { phone = value.Substring(0, 10); ext = value.Substring(10, value.Length - 10); } // format switch (phoneFormat) { case PhoneFormat.Dots: phone = String.Format(@"{0:###\.###\.####}", double.Parse(phone)); break; case PhoneFormat.Dashes: phone = String.Format(@"{0:###-###-####}", double.Parse(phone)); break; default: phone = String.Format(@"{0:(###) ###-####}", double.Parse(phone)); break; } // add ext if there is one if (ext.Length > 0) { phone = phone + extPrefix + ext; } return(phone); }
public static string PhoneNumber(PhoneFormat format) { switch (format) { case PhoneFormat.NANP: { return($"{Areacode}-{LocalCode}-{LastFour}"); } default: return(PhoneNumber()); } }
public static string PhoneNumber(PhoneFormat format) { var areaCode = RandomValue.Int(999, 200); var localCode = RandomValue.Int(999, 100); var lastFour = RandomValue.Int(9999, 1000); switch (format) { case PhoneFormat.NANP: { return($"{areaCode}-{localCode}-{lastFour}"); } default: return(PhoneNumber()); } }
public PhoneValidator(PhoneFormat format) { this.format = format; }
/// <summary> /// Checks a phonenumber with the specified format. /// </summary> /// <param name="phoneNumber"></param> /// <param name="format"></param> /// <returns></returns> public static bool CheckPhoneNumber(string phoneNumber, PhoneFormat format) { if (!string.IsNullOrEmpty(phoneNumber)) { switch (format) { case PhoneFormat.France: return RegexCheck(phoneNumber, PhoneNumbers.France); case PhoneFormat.Germany: return RegexCheck(phoneNumber, PhoneNumbers.Germany); case PhoneFormat.Japan: return RegexCheck(phoneNumber, PhoneNumbers.Japan); case PhoneFormat.China: return RegexCheck(phoneNumber, PhoneNumbers.China); case PhoneFormat.America: return RegexCheck(phoneNumber, PhoneNumbers.America); case PhoneFormat.India: return RegexCheck(phoneNumber, PhoneNumbers.India); case PhoneFormat.Spain: return RegexCheck(phoneNumber, PhoneNumbers.Spain); case PhoneFormat.UnitedKingdom: return RegexCheck(phoneNumber, PhoneNumbers.UnitedKingdom); case PhoneFormat.Brazil: return RegexCheck(phoneNumber, PhoneNumbers.Brazil); case PhoneFormat.Dutch: return RegexCheck(phoneNumber, PhoneNumbers.Dutch); case PhoneFormat.Australia: return RegexCheck(phoneNumber, PhoneNumbers.Australia); case PhoneFormat.Israel: return RegexCheck(phoneNumber, PhoneNumbers.Israel); case PhoneFormat.NewZealand: return RegexCheck(phoneNumber, PhoneNumbers.NewZealand); case PhoneFormat.Russia: return RegexCheck(phoneNumber, PhoneNumbers.Russia); case PhoneFormat.Invariant: return RegexCheck(phoneNumber, PhoneNumbers.Invariant); case PhoneFormat.Sweden: return RegexCheck(phoneNumber, PhoneNumbers.Sweden); case PhoneFormat.Italy: return RegexCheck(phoneNumber, PhoneNumbers.Italy); case PhoneFormat.Denmark: return RegexCheck(phoneNumber, PhoneNumbers.Denmark); default: return false; } } return false; }