NSRegularExpression validate email

后端 未结 3 1708
长情又很酷
长情又很酷 2020-12-13 22:29

i want to use the NSRegularExpression Class to validate if an NSString is an email address.

Something like this pseudocode:



        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 23:02

    This was an older question, but it's general enough to never go out of style.

    99.9% of email address regexs are wrong and even fewer handle international domains (IDN) properly.

    Trying to make a comprehensive email regex isn't a good use of development time or user CPU cycles.

    This is much simpler:

    1. Match [^@]+@([^@]+)

    2. Extract the domain per the matched part.

    3. Fail if it matches a banned domain. Cache the list of banned domains locally from a JSON endpoint so you don't have to wait for re-approval and can update it on-the-fly. (Be sure to log web stats for conversion analysis.)

    4. Check that the domain has DNS records. Check A and AAAA, in addition to MX, because not all domains implement MX. (Lame but true.)

    5. Just try to send the email already. The user will try again if they perceive an app has value. :))

提交回复
热议问题