Applying Mask to TextField-part deux

Please pardon my starting a new thread, but I had prematurely indicated my previous thread was answered. I have not been able to get textField1.format = " “\(###) ###\-####”" placed in the lostFocus event to work satisfactorily. For a reason I have yet to discern, that technique sometimes leaves the characters ()-0 in an empty textfield. I have also not been happy with using the mask property as the variations for a phone number field are many. nnn-nnn-nnnn, nnn-nnnn, nnn-nnn-nnnn ext nnn, etc, etc.
I am posting to make a plea for guidance from someone who has successfully created a phone number field which forces the user to a professional looking display.
Thanks in advance for any ideas.

Hi roger.
For the b-0[/b] a trick is something like this.
on the lostfocus event.

if len(me.text)=0 then me.Format ="" end
Now about the format maybe a class or elseIf or Select case with different mask and formats?
A second tip is to add a PopUp Menu next to textfield with (width 20) and ask the user to select the format he want?
Based on the user selection create the format and mask :wink:

Thanks again, Leannis.

[quote]if len(me.text)=0 then
me.Format =""
end[/quote]
I had tried that already and it makes no difference.
I give up on this. I’m just going to present the textfield to the user and let them enter the phone number however they like.
I appreciate your efforts to help.

For me the above code working…
Here is a small example i have make with formats maybe you get some ideas.
Is very simple but working :slight_smile:
example

A long while back I created a subclass on the textfield that automatically formats as a phone number as the user types into it. It allows for a 5-digit extension also. See the Notes in the Class for details.

Phone Formatting Textfield

I was able to change my Dropbox acc’t to get your example. The problem as I see it is still the one that I was wrestling with. Your example places the ( automatically at the beginning of the field. If the user deletes that, then the field will not accept further input until the “(” is re-entered. That could be confusing to a user who uses the format nnn-nnn-nnnn or does not wish to enter an area code.
Thanks for sharing your example.

I had a similar issue where with the format “####-####-####-####-####” the TF showed “0” when not focused.

I dragged a regular TextField in my project, then set the Mask property in TextChange :

Sub TextChange() if me.Text = "" then Me.Mask ="" Me.Format = "" else Me.Mask = "####-####-####-####-####" Me.Format = "####-####-####-####-####" end if End Sub

In Keydown, I filter out all characters but numeric, “-”, delete and arrows left and right :

Function KeyDown(Key As String) As Boolean if instr("1234567890-"+Chr(8)+chr(28)+chr(29), key) = 0 then return true end if End Function

[quote=268861:@Roger Clary]I was able to change my Dropbox acc’t to get your example. The problem as I see it is still the one that I was wrestling with. Your example places the ( automatically at the beginning of the field. If the user deletes that, then the field will not accept further input until the “(” is re-entered. That could be confusing to a user who uses the format nnn-nnn-nnnn or does not wish to enter an area code.
Thanks for sharing your example.[/quote]
True. As far as I can see, to give you the total flexibility you seem to want will require the field to be free-form on input and do all of the validity checking and formatting on the exit (lostFocus event). If you mask it for formatting then you have to deal with having the user follow the rules of the field. when I designed my field object, my aim was for a data entry person to be able to enter the numbers without having to worry about the format characters.

Good luck and if you solve this, be sure to share.

Dale

Just because I wanted a fun project I made you a text field that works like dialing on an iPhone. As Dale’s intent was, this field is so you can just type numbers. iPhoneField.xojo_binary_code

What makes this unique is that it sees the phone number as a string and formats the string. This removes the mask issue.

That’s because Format is specifically for turning a number into a formatted string. I saw this when I was experimenting, but realized how it makes sense because of the input Format takes.

Indeed, but the workaround I posted makes it much less cruel. In particular, I hated the beep when typing the wrong key. That is so early 20th century IBM 2250.

Thanks to everyone who replied. As I discovered, there are many variations of an acceptable phone number field. Tim’s example suited my needs the best. I will provide the requested attribution in my ‘About’

I wanted to revive this thread to point to my open source project Phone Number Auto Formatting Textfield Class. I solves the issues stated above and provides a nice auto formatting text class for say Twilio To/From numbers.

https://forum.xojo.com/59548-open-source-phone-number-auto-formatting-textfield-class