简体   繁体   中英

C# Monotouch phone number formatting in UITextField

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 ie I have the regex part covered.

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 ValueChanged event 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 EditingDidEndOnExit event 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM