0


I've got an UITextField and I need to allow only numbers, comma or point typing in this field.
How can I check if this string contains any character instead of these above and how can I replace/delete them?

My field will store a cost and I need only numbers, comma or point...

Can anyone help me?
Thanks!

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
matteodv
  • 3,992
  • 5
  • 39
  • 73
  • you can see this sample ... http://cocoabugs.blogspot.com/2011/01/allowing-only-characters-and-numbers-in.html –  Jan 07 '11 at 04:05

4 Answers4

2

For the textfield, you could use the UITextFieldDelegate and implement the textField:shouldChangeCharactersInRange:replacementString: method. According to the docs, "the text field calls this method whenever the user types a new character in the text field or deletes an existing character." In the method, you should do a check for all non-number characters and remove them.

donkim
  • 13,119
  • 3
  • 42
  • 47
  • I want to create a scenario like the one described in the comment on Matthew Frederick's answer... Thanks however! – matteodv Dec 11 '10 at 18:38
1

You can use the -textFieldDidEndEditing: method from the UITextFieldDelegate protocol. This method is called whenever a text field resigns the first responder status (i.e. ends editing). This is the chance to check the entered String. If you want to perform a check after each character that has been input, use -textField:shouldChangeCharactersInRange:replacementString: as donkim suggests.

Björn Marschollek
  • 9,899
  • 9
  • 40
  • 66
0

Instead to check the string why cant you assign the keyboard value of the textfield as NumberPad. So that the user will not be able to type a string.

ipraba
  • 16,485
  • 4
  • 59
  • 58
  • They'll still be able to paste whatever they want. – Matthew Frederick Dec 11 '10 at 18:05
  • I thought about this and now I'm using the NumberPad... But I need to be able to type a comma or a point, based of the locale... Is there a way to customize the number pad to add another button? – matteodv Dec 11 '10 at 18:05
  • @Matthew Frederick I also will prevent this... Is there a way to customize the keyboard or to check this string? – matteodv Dec 11 '10 at 18:07
  • I dont know exactly the logic but with NSScanner and NSCharacterset you can check with the String and Numbers. Instead of numberpad use numbers and punctuation value in keyboard. – ipraba Dec 11 '10 at 18:12
0

You can do it by monitoring user input in the field and automatically removing non-matching characters. Create an IBAction method:

- (IBAction)validateEntry:(id)sender;

then attach it to the text field's Editing Changed event. The method will be called every time the text in the field changes, whether from the user typing or from the user pasting text in.

Inside the method you can examine the text and remove offending characters or, more politely, update a UILabel that tells the user they've entered invalid characters and disable any submit buttons until it's fixed.

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • Unfortunately I don't use IB... I'll do a thing like this scenario: an user type somathing in the text field, also with letters or other characters. But, when he tap on the save button, the string must be checked and if it contains not allowed characters, an alert view will appear... Is it possible to do this? However thanks for the answer! – matteodv Dec 11 '10 at 18:36