1

How do i format or create a UITextField (using Xamarin/C#) which allows phone number entry as "(xxx)xxx-xxxx"

The parentheses and the hyphen can be dynamic or static. Not very rigid on that. I could create three different UITextFields for the 3-part phone and have labels for "(", ")", and "-";but that doesn't look like a good way of doing this. Would rather do it correctly than hack it.

Any suggestions or ideas? There must be a better way. I found this: UITextField for Phone Number

but i don't understand objective-c. Maybe someone can help translate, if that is the solution i.e. I have the regex part covered.

Community
  • 1
  • 1

1 Answers1

2

Depends on your specific requirements:

  1. Do you want the text to be formatted as the user is typing?
  2. Do you want the text to be formatted once the user completes typing?

To accomplish either of the above scenarios, all you need to do is subscribe to the appropriate event and perform formatting logic.

For scenario 1: Subscribe to the ValueChangedevent on the UITextField. This event will be fired everytime a character is added or removed. Now you can define your code to format the phone number as the user is typing

For scenario 2: Subscribe to the EditingDidEndOnExitevent on the UITextField. This event will be fired one editing has completed and the text field is no longer in focus. Now you can define your code to format the phone number here

pnavk
  • 4,552
  • 2
  • 19
  • 43