Search code examples
iosregexswiftemailresearchkit

ResearchKit: Validate email


I'm attempting to create a form step where one of the form step items is an email input. For this I want to validate the email against certain domains i.e.

@gmail.com, @icloud.com, @me.com

I can see we have an email answer format in the form of this:

ORKEmailAnswerFormat()

However I can't see anywhere in this type that allows me to apply a validation regex. Looking into this I see we have the following

ORKAnswerFormat.textAnswerFormatWithValidationRegex(validationRegex, invalidMessage)

I suppose this is my best option? If so, would anyone know of a regex (my regex isn't the greatest!) in swift that would handle the 3 domains stated above?

I have something like this...(not the greatest i know!)

[A-Z0-9a-z._%+-][email protected]


Solution

  • [A-Z0-9a-z._%+-]+@(?:icloud|me|gmail)\.com
    

    (or, if you don't care about capturing:)

    [A-Z0-9a-z._%+-]+@(icloud|me|gmail)\.com
    

    Now I made two modifications. I escaped the . and I made it so that the other two domains are options.

    I suggest that you convert the whole thing to lower case. I don't know Swift, but you may be able to use one of its functions or the i modifier:

    (?i)[0-9a-z._%+-]+@(icloud|me|gmail)\.com