NetTalk Central

Author Topic: Formatting a phone number after user enters field  (Read 22169 times)

Mike Grigsby

  • Sr. Member
  • ****
  • Posts: 380
    • Yahoo Instant Messenger - onthedotsoftware
    • View Profile
    • MyHomeAssets! Software (among others)
Formatting a phone number after user enters field
« 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.
« Last Edit: July 16, 2008, 10:47:54 PM by Mike Grigsby »
Mike Grigsby
Credify Systems
Central Oregon, USA

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Formatting a phone number after user enters field
« Reply #1 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


Mike Grigsby

  • Sr. Member
  • ****
  • Posts: 380
    • Yahoo Instant Messenger - onthedotsoftware
    • View Profile
    • MyHomeAssets! Software (among others)
Re: Formatting a phone number after user enters field
« Reply #2 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.
Mike Grigsby
Credify Systems
Central Oregon, USA

dcpeders

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: Formatting a phone number after user enters field
« Reply #3 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

terryd

  • Hero Member
  • *****
  • Posts: 759
    • View Profile
    • Davcomm
    • Email
Re: Formatting a phone number after user enters field
« Reply #4 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))
Terry Davidson
Windows 10 64 bit/Windows7 64bit
Clarion 9.1.11529/Clarion10 12567
Nettalk 913
Nettalk 1015
StringTheory267/Winevent515/XFiles298/MessageBox239/Cryptonite186

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Formatting a phone number after user enters field
« Reply #5 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

terryd

  • Hero Member
  • *****
  • Posts: 759
    • View Profile
    • Davcomm
    • Email
Re: Formatting a phone number after user enters field
« Reply #6 on: June 29, 2013, 08:29:10 AM »
Hi Bruce.
Yes I know that setting. I was more interested in the principle.
Terry Davidson
Windows 10 64 bit/Windows7 64bit
Clarion 9.1.11529/Clarion10 12567
Nettalk 913
Nettalk 1015
StringTheory267/Winevent515/XFiles298/MessageBox239/Cryptonite186