NetTalk Central

NetTalk Web Server => Web Server - Share Knowledge => Topic started by: Mike Grigsby on July 16, 2008, 10:44:34 PM

Title: Formatting a phone number after user enters field
Post by: Mike Grigsby on July 16, 2008, 10:44:34 PM
I wanted to be able to take the user input for a phone number (USA) and convert it to a formatted number, no matter how the user entered it. For example, 404.555.1234, or 404-555.1234, for example, would be nicely formatted to

(404) 555-1234

To do this, I did the following:

1. In the field template, on the Client Side tab, I added the field name to the Reset List and checked the Value field. (E.g., in the FIL:Phone field, I added itself to the reset list)

2. I added a string to the local data: L:String.

3. I added the following code to the embed:
Routines | 'Your Tab' | Field Name | 3 Value Routine

L:String=p_web.RestoreValue('FIL:Phone')
IF L:String
    i#=0
    n"=' '
    LOOP LEN(CLIP(L:String)) TIMES
      i#+=1
      IF INSTRING(SUB(L:String,i#,1),'1234567890',1,1)
        n"=CLIP(n") & SUB(L:String,i#,1)
      END !IF L:INS...
    END !Loop

    p_web.SetSessionValue('FIL:Phone', FORMAT(n" , @p(###) ###-####p))
END !IF L:Str...


Now when the user tabs off the phone field, it formats the number correctly.
Title: Re: Formatting a phone number after user enters field
Post by: Bruce on July 18, 2008, 03:23:52 AM
Hi Mike,

An excellent report, thanks for sharing.

There is one item which comes out of this which is worth discussing.

You used the "Value" routine for the field, and that's fine, but perhaps the more correct one to use would have been the Validate routine.

In "concept" the Validate routine is used to massage the field, check it's ok and so on, and the Value routine is used to display it.

So the Validate is _not_ called when the form opens, but it is called if the field is changed. The Value routine is called when the form is opened, and whenever the field is reset.

cheers
Bruce

Title: Re: Formatting a phone number after user enters field
Post by: Mike Grigsby on July 18, 2008, 07:27:25 AM
Thanks Bruce, That is immensely helpful as it explains why things appear the way they do. This is why I'm trying to share some of the things I'm learning because I need to know if there are better ways of doing things that won't get me in trouble when I release it in a product.
Title: Re: Formatting a phone number after user enters field
Post by: dcpeders on June 20, 2013, 11:54:45 AM
Why not just use "deformat"? It works fine as long as you put in a picture. The following code works with  any style phone number input, such as - 999.123.6543 or 345-123-4325 or (333) 123.6543

    N" = '999.555.1234'           !P_WEB.RESTOREVALUE('FIL:PHONE')
    IF N"
        P_WEB.SSV('L:STRING', DEFORMAT(N", @P################################P))
        N" = P_WEB.GSV('L:STRING')
        P_WEB.SSV('L:STRING', FORMAT(N", @P(###) ###-####P))
        MESSAGE('Phone number - '&p_web.gsv('l:string'))
    END

Dave Pederson
Title: Re: Formatting a phone number after user enters field
Post by: terryd on June 29, 2013, 02:45:09 AM
Mike
Thanks for this.
Questions
Dave wouldn't your code format a character into the phone number?

Mike Is there a good reason to use p_web.RestoreValue() instead of p_web.GSV(). I am using p_web.GSV() throughout my code so I am just wondering if there is a reason to use restorevalue rather than gsv.

I like your idea ( reminds me of what we had to do in NT4 to set upper and Capitalize before Bruce added it to the Validation tab)
One use I have for it is when I want to  display an entered number as formatted 000001
If I set the picture display to '@N06' the field displays as '000000' before any entry. This isn't  a problem in Chrome where the whole field is highlighted and you can start typing straight away, but in Firefox you have to select and highlight the field before you can enter if it is the first field in a form. I am spending a lot of time in my screens trying to do anything in the capture to stop the user having to use a mouse (we look to capture 2000-4000 insurance policies a day and anything that slows this down is a no-no) and this allows me to take away the picture, convert the value to a fixed layout, and send it back formatted correctly.
L:String=p_web.GSV('LOC:PolicyNumber')
IF L:String
    i#=0
    n"=' '
    LOOP LEN(CLIP(L:String)) TIMES
        i#+=1
        IF INSTRING(SUB(L:String,i#,1),'1234567890',1,1)
        n"=CLIP(n") & SUB(L:String,i#,1)
        END !IF L:INS...
    END !Loop
    p_web.SetSessionValue('LOC:PolicyNumber', FORMAT(n" , @N06))
Title: Re: Formatting a phone number after user enters field
Post by: Bruce on June 29, 2013, 05:57:52 AM
>> If I set the picture display to '@N06' the field displays as '000000' before any entry.

try'@N06b'

cheers
Bruce
Title: Re: Formatting a phone number after user enters field
Post by: terryd on June 29, 2013, 08:29:10 AM
Hi Bruce.
Yes I know that setting. I was more interested in the principle.