App has crashed again on textfield delegate

I am trying to setup a textfield on my Register 2 screen so that when I type in 2125551212 it autoformats to (212) 555-1212. I was attempting to use the Should Change Characters in range but when I put in a true/false variable, the app crashed again, I cannot open my project, can you please fix the error for me?

Additionally, what is the documentation or steps to perform this? I am trying to follow guidlines for Swift, but they don’t match.

Hi,
we have fixed your app and it should not crash anymore. It looks like you deleted a Function that was used in an Event Action.

Thanks
Kostas

In order to implement the requested feature you can use something like that

Specifically, you can set the value of the Text Field to an input property and remove any unwanted characters such as parentheses and hyphens by replacing them with an empty character. Then, format the number by adding the opening parentheses, the first three digits of the input, the closing parentheses, and the hyphen. Finally, set the formatted number as the text of the Text Field.

This Function can be connected to the When End Editing action of the TextField

Thanks
Kostas

Thank you for this example.

Is it possible to implement this feature wall typing? Meaning that when somebody clicks on the phone number fill them just starts typing their number it starts formatting automatically? I assume you mean I could put this function inside of the textfield did change?

Also, what would be required to restrict the characters in the text field meaning with the parentheses and scores, there are 14 characters total how can I limit to text field to know accept more than 14?

Can you send me instructions on how to get the Textfield protocols to work? The problem i am having is that I want to limit the characters of the text fields, and also prevent numbers in 2 of them and prevent letters in another. I know how to do it in swift, but its not making sense here.

The function you gave me works, but you can’t backspace if you make an error with the number while typing, and without being able to limit the characters in the field, It won’t work on did end editing.

Hi

Yes, if you want to have the function run every time a user writes or deletes a character, you can use the When Text Change Event.

Here is also a revised function doing exactly this

You can notice that the first code block is a different Function called Get Text Field Input Without Unwanted Characters. This is the following function and it returns a clean text without the (,), , -.

There are many ways to restrict it. One of them is to read the input every time a character changes in When Text Change Event and then write back only the first 14 characters. This is what the first function is doing.

In order for the TextField Protocols to work you need to NOT use any Event Action for that TextField.
So if you want to use the following protocol function


You will need to not use the When Text Change Event and change your code to work using the protocol one.

You are correct and we adjusted so it should work now.

I am trying to set up a function like in the first screenshot, but I cannot figure out how to get it to give a return.

I figured out the return argument, I am working on this now.

So this worked well on the phone number field, thank you. The problem I have now, is I have 2 text fields for entering a first and last name and I need to make them ONLY allow numbers and spaces. Additionally I have to limit the amount of characters to 50.

I tried to implement a function using this above code and it cause the app to crash. How can I restrict the text field to ONLY letters or ONLY numbers and put a character limit on it?

Could you please send me the function that you used?

Hi again,
here is an example on how you can do what you want.

First you need to create a function that returns a List of Texts with all valid characters
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z

Then you will use this function in a Text Field Text Changed function.

In this function you iterate through all the valid characters and replace all the matching characters of the input with the empty string(Note: Not space character). So after the for if the input had only allowed characters, it will be empty, as all the characters would have been replaced.

This is extremely helpful, thank you! I assume, I can do the same thing with numbers?

I will work on this and the locations today

Yes, just use a different function for numbers and use this text
0-1-2-3-4-5-6-7-8-9