How to remove non numeric characters from phone number in objective-c?

前端 未结 5 694
不思量自难忘°
不思量自难忘° 2021-01-01 19:05

This is my first attempt to make an ios app.

I\'m using people picker to ask the user for a phone number, but when it retrieves with the code below, my NSStrin

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 19:43

    I think there are ways to solve this:

    1. Using NSRegularExpression to remove anything but numbers. You can see here or here to know how to validate phone number.
    2. Write your own scanner to remove characters you don't need. Remove blanks or remove all but numbers.
    3. Use the UITextFieldDelegate, write the textField:shouldChangeCharactersInRange:replacementString: method, check the replacement string if it is in the range of 0-9.

    Hope helps.

提交回复
热议问题