4

A user enters his/her number of credit hours per course in my UITextField in this format....

4,3,4,3,3

(4 credit hours for 1st course, 3 credit hours for 2nd course, so on)

Conditions: The user can only enter in MIN of 1 course and MAX 5 courses, the credit hours must be separated by a comma except for the last one, the string should contain only numbers and commas (no letters etc.) in that order.

I am trying to display an error message if conditions are violated. I think I will need to create a regExp. I am not sure how to create one since the user can enter in 2 courses so... (3,4) and still be okay. (like it cant follow a strict format like phone number).

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
bean22
  • 41
  • 1

1 Answers1

2

Rather than letting the user enter what they think is correct and then telling them they're wrong at the end, consider implementing the text field delegate method textField:shouldChangeCharactersInRange:replacementString:. This doesn't preclude you using the regex, but you can also filter the characters typed by the user to prevent any letters and inform the user immediately when they make a mistake.

Wain
  • 118,658
  • 15
  • 128
  • 151