KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility to test a phone number as a valid North American phone number
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: May 27, 2015

Need to make sure that phone number the user just entered is a valid North American phone number? The utility will check the submission in most accepted formats and return TRUE if it passes the test. It does not check phone number plus extension so only the phone number itself should be passed.

If (True)
   If (False)
      Begin SQL
         /*
          Name: UTIL_ValidPhoneNumber_NorthAm
          Path: UTIL_ValidPhoneNumber_NorthAm

          Purpose: If phone number ends with pattern of
          "###-###-####", "### ### ####", "(###) ### ####" or ##########
          Area code and exchanges do not begin with a "0" or "1"
          hense character class [2-9]

          $0 - BOOLEAN - True = valid, False = invalid
          $1 - TEXT - Phone number to test
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_BOOLEAN($0)
   C_TEXT($Phone_T;$1)
    //--------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$Pos;$Len)
   C_TEXT($Pat_T)
End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters

// Trim any leading or trailing white space, see KB asset 76068
//
$Phone_T:=STR_Trim ($1)

//======================== Method Actions ==================================

// "\\D*" = Find zero or more non digit characters
// "([2-9]\\d{2})" find three digits that do not start with a 0 or 1
// "(\\d{4})" find four digits, 0 - 9
// "$" at the end if the string, no characters followiing
//
$Pat_T:="\\D*([2-9]\\d{2})\\D*([2-9]\\d{2})\\D*(\\d{4})$"

//======================== Clean up and Exit =================================

$0:=Match regex($Pat_T;$Phone_T;1;$Pos;$Len)