LC converts phone number to scientific notation

Bob Sneidar bobsneidar at iotecdigital.com
Fri Sep 9 12:45:32 EDT 2022


OK As it turns out, it is nearly impossible to format a series of digits to an international format, separating the different parts by delimiters like - and (). Refer to web link for details.

https://www.tipard.com/mobile/international-phone-number-format.html

In short, the country code can be 1 to 3 digits, the dial out code *should* be 00 but some countries don't comply with the standard, and use a different code, which can be 2 to 3 digits, etc.

To internationalize formatPhone() I would need a database of each country and how they format their phone numbers, and boo on that idea.

Here is my formatPhone() code as it stands for US and Canada. (I think it is included in the Master Library as well.) If the phone number is not not 7 or 10 digits after extracting the extension, then it returns the original value.

FUNCTION formatPhone thePhoneNumber, theFormat
   -- text
   -- mid
   IF theFormat is empty THEN
      put "classic" into theFormat
   END IF

   IF thePhoneNumber is empty THEN
      return thePhoneNumber
   END IF

   if "ext" is in thePhoneNumber then
      put offset("ext", thePhoneNumber) into tChar
      put char tChar to -1 of thePhoneNumber into theExtension
      put char 1 to tChar -1 of thePhoneNumber into thePhoneNumber
   else if "x" is in thePhoneNumber then
      put offset("x", thePhoneNumber) into tChar
      put char tChar to -1 of thePhoneNumber into theExtension
      put char 1 to tChar -1 of thePhoneNumber into thePhoneNumber
   end if

   REPEAT WITH i = 1 to the number of chars of thePhoneNumber
      IF char i of thePhoneNumber is a number THEN
         put char i of thePhoneNumber AFTER theNewValue
      END IF
   END REPEAT

   put the length of theNewValue into theLength

   IF theLength is not among the items of "7,10" THEN
      return thePhoneNumber && theExtension
      exit formatPhone
   END IF

   put "-" BEFORE char -4 of theNewValue

   IF theLength is 10 THEN
      IF theFormat is "classic" THEN
         put ") " AFTER char 3 of theNewValue
         put "(" BEFORE theNewValue
      ELSE
         put "-" AFTER char 3 of theNewValue
      END IF
   END IF

   IF theExtension is not empty THEN put " " & theExtension AFTER theNewValue
   return theNewValue
END formatPhone

Bob S


On Sep 9, 2022, at 08:51 , Klaus major-k via use-livecode <use-livecode at lists.runrev.com<mailto:use-livecode at lists.runrev.com>> wrote:

Hi Bob,

Am 09.09.2022 um 17:38 schrieb Bob Sneidar via use-livecode <use-livecode at lists.runrev.com<mailto:use-livecode at lists.runrev.com>>:

I have a function called formatPhone() I can modify it to support international notation and send it to you.

thank you, will then store it for future use.



More information about the use-livecode mailing list