Is it possible to check against a regex in the -shouldChangeCharacters method?

Discussion in 'iOS Programming' started by chrono1081, Aug 14, 2014.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    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?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     

Share This Page