1

I have this function where I need to get the 10 digit phone number.

<cffunction name="StandardPhoneNumber" 
                access="public" 
                hint="Returns a 10 digit phone number in a numeric format ex. 8883334444"

                Description="We use a standard numeric only phone number. We stripped the non numeric characters and return 10 digit">
        <cfargument name="phoneNumber" required="true" >

        <!--- we need a little bit of processing here coz of a bug where a space is returned at the start of the number  --->
        <cfset local.phoneNumber = right(REReplace(arguments.phoneNumber, "[^0-9]", "", "ALL"),10) />

        <cfreturn trim(local.phoneNumber)/>
</cffunction>

For some reason the returned value is also returning some space.

Example input value '+5122131151' will return ' 5122131151' (a space in front of 5)

The rereplace function is added

REReplace(arguments.phoneNumber, "[^0-9]", "", "ALL")

so that it can accept phone number even with dashes +1888-333-4444 and return 10 digit phone number. This is for US phone number only where the standard phone number is 10 digit.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Vlad
  • 1,077
  • 2
  • 13
  • 27

1 Answers1

1

First of all: no, your function is not the culprit. The regular expression [^0-9] strips everything non-digit, this includes whitespaces. The trim at the end of your function is redundant and not even needed. Here is a fiddle to prove it. if link is dead, see attachment at the end

When + becomes a whitespace (space character), it is more likely related to being an encoding issue. The literal character + is replaced to a space when it occurs in a GET query string or POST payload (more details). So I assume something is wrong in your input/output chain, unrelated to the function.

Also, I'm not an expert on US phone numbers and how to properly handle the country calling code, but your approach seems a bit too naive. For example: If the phone number is less than 10 digits, you would keep the 1 of the country code +1, since right(number, 10) would still contain it. It's even worse when you have to deal with international numbers, because the country calling code can be more than a single digit and have special rules (e.g. +49 for Germany, and then strip a following 0, if present). You might want to consider this.

Anyway, check where you call StandardPhoneNumber(), check what becomes of ther result you pass and check what you output in the end. The culprit is hiding somewhere in this chain.

Attachment

<cfset valuesToTest = [
    "1234567890",
    "123-4567-890",
    "+1234567890",
    "+123-4567-890",
    "123",
    "12345678901234567890",
    "  1234567890#chr(9)#"
]>
<cfoutput>
<cfloop array="#valuesToTest#" index="value">
    <cfset newValue = StandardPhoneNumber(value)>
    #value# = #newValue# (length: #len(newValue)#)<br>
</cfloop>
</cfoutput>

<cffunction name="StandardPhoneNumber" 
            access="public" 
            hint="Returns a 10 digit phone number in a numeric format ex. 8883334444"

            Description="We use a standard numeric only phone number. We stripped the non numeric characters and return 10 digit">
    <cfargument name="phoneNumber" required="true">

    <cfset local.phoneNumber = right(REReplace(arguments.phoneNumber, "[^0-9]", "", "ALL"), 10)>

    <cfreturn local.phoneNumber>
</cffunction>
Alex
  • 7,743
  • 1
  • 18
  • 38
  • I've added the trim because I'm getting whitespace at the beginning even regex has done its job which puzzles me that's why its redundant and added. Your suggestions are correct but still doesn't solve my problem of whitespace in the beginning after running the function. The + sign is probably the culprit here – Vlad Aug 19 '19 at 08:28
  • @Vlad Check the fiddle I mentioned. The function correctly trims whitespaces. If you suddely have whitespace in front of the returned value, something else is adding it back. Either you post code of you actually using the function or dig in deeper in your own code. – Alex Aug 19 '19 at 11:56
  • I will probably remove the trim since its not helping. I added urldecode to turn the + to %2b. It does seems to fix my problem. – Vlad Aug 19 '19 at 21:31