Search code examples
iphoneuitextfieldcharacterletters-and-numbers

Check if a string contains letters and cancel them



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!


Solution

  • 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.