Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,899
6,518
Isla Nublar
Hi guys,

Right now I have validation occurring in two spots and I'd love to narrow it down to one but I'm pretty stuck and haven't figured out a solution.

What I want to do is check the characters currently being entered against a regular expression. According to Apples documentation you have to put any checking code in the shouldChangeCharactersInRange method, but I can't find a working way to check against a regex in there.

For example, say I want to check social security numbers and my regex looks like this: ^[0-9]{3}[0-9]{2}[0-9]{4}|[0-9]{3}-[0-9]{2}-[0-9]{4}$

How would I setup -shouldChangeCharactersInRange to check against the regex while characters are being input? Is this even possible?
 
Can you be more clear on what you expect the app to do?

Your regex simply looks for a string with 9 digits or 9 digits with hyphens in it. What do you want to happen when the user has only typed a single digit, two digits, three digits?

If you explain what you want to happen then you can write code that achieves that.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.