Source/NFX/Parsing/DataEntryUtils.cs
/// <summary>
/// Normalizes US phone string so it looks like (999) 999-9999x9999.
/// </summary>
Normalizes US phone string so it looks like (999) 999-9999x9999.
Normalizes US phone string so it looks like ( 999 ) 999 - 9999x9999 .
public static string NormalizeUSPhone(string value)
    {
      if (value == null) return null;
      value = value.Trim();
      if (value.Length == 0) return value;
      bool isArea = false;
      bool isExt = false;
      string area = string.Empty;
      string number = string.Empty;
      string ext = string.Empty;
      if (value.StartsWith("+")) 
        return value;
      Char chr;
      for (int i = 0; i < value.Length; i++)
      {
        chr = value[i];
        if (!isArea && chr == '(' && area.Length == 0)
        {
          isArea = true;
          continue;
        }
        if (isArea && chr == ')')
        {
          isArea = false;
          continue;
        }
        if (isArea && area.Length == 3)
          isArea = false;
        if (number.Length > 0 && !isExt)
        {  
          if (chr == 'x' || chr == 'X' || (chr == '.' && number.Length>6))
          {
            isExt = true;
            continue;
          }
          string trailer = value.Substring(i).ToUpperInvariant();
          if (trailer.StartsWith("EXT") && number.Length >= 7)
          {
            isExt = true;
            i += 2;
            continue;
          }
          if (trailer.StartsWith("EXT.") && number.Length >= 7)
          {
            isExt = true;
            i += 3;
            continue;
          }
        }
        if (!Char.IsLetterOrDigit(chr)) continue;
        if (isArea)
          area += chr;
        else
          if (isExt)
            ext += chr;
          else
            number += chr;
      }
      while (number.Length < 7)
        number += '?';
      if (area.Length == 0)
      {
        if (number.Length >= 10)
        {
          area = number.Substring(0, 3);
          number = number.Substring(3);
        }
        else
          area = "???";
      }
      if (number.Length > 7 && ext.Length == 0)
      {
        ext = number.Substring(7);
        number = number.Substring(0, 7);
      }
      number = number.Substring(0, 3) + "-" + number.Substring(3);
      if (ext.Length > 0) ext = "x" + ext;
      return string.Format("({0}) {1}{2}", area, number, ext);
    }
public static string NormalizeUSPhone ( string value ) { if ( value == null ) return null ; value = value . Trim ( ) ; if ( value . Length == 0 ) return value ; bool isArea = false ; bool isExt = false ; string area = string . Empty ; string number = string . Empty ; string ext = string . Empty ; if ( value . StartsWith ( " " ) ) return value ; Char chr ; for ( int i = 0 ; i < value . Length ; i ++ ) { chr = value [ i ] ; if ( ! isArea && chr == ' ' && area . Length == 0 ) { isArea = true ; continue ; } if ( isArea && chr == ' ' ) { isArea = false ; continue ; } if ( isArea && area . Length == 3 ) isArea = false ; if ( number . Length > 0 && ! isExt ) { if ( chr == ' ' || chr == ' ' || ( chr == ' ' && number . Length > 6 ) ) { isExt = true ; continue ; } string trailer = value . Substring ( i ) . ToUpperInvariant ( ) ; if ( trailer . StartsWith ( " " ) && number . Length >= 7 ) { isExt = true ; i += 2 ; continue ; } if ( trailer . StartsWith ( " " ) && number . Length >= 7 ) { isExt = true ; i += 3 ; continue ; } } if ( ! Char . IsLetterOrDigit ( chr ) ) continue ; if ( isArea ) area += chr ; else if ( isExt ) ext += chr ; else number += chr ; } while ( number . Length < 7 ) number += ' ' ; if ( area . Length == 0 ) { if ( number . Length >= 10 ) { area = number . Substring ( 0 , 3 ) ; number = number . Substring ( 3 ) ; } else area = " " ; } if ( number . Length > 7 && ext . Length == 0 ) { ext = number . Substring ( 7 ) ; number = number . Substring ( 0 , 7 ) ; } number = number . Substring ( 0 , 3 ) + " " + number . Substring ( 3 ) ; if ( ext . Length > 0 ) ext = " " + ext ; return string . Format ( " " , area , number , ext ) ; }
Normalizes US phone string so it looks like (999) 999-9999x9999.
Normalizes US phone string so it looks like ( 999 ) 999 - 9999x9999 .
//international phone, just return as-is //check extention
value
string