Can it cause harm to validate email addresses with a regex?

后端 未结 8 866
旧巷少年郎
旧巷少年郎 2020-12-06 03:55

I\'ve heard that it is a bad thing to validate email addresses with a regex, and that it actually can cause harm. Why is that? I thought it never could be a bad thing to val

8条回答
  •  执念已碎
    2020-12-06 04:25

    Regex is not harmful.

    Use a good email regex to filter the impatient fake user.

    If you are selling to that individual, you might want to contact them
    for further validation, though sellers don't care about email too much
    and just validating the credit card is good enough for them.

    Otherwise, the only other place where validation is necessary is when
    someone wants access to and interact with your forum, and for some reason
    you want get remuneration by selling their email to mass advertisers,
    even though you say you won't do that.

    A general email regex in the html5 spec is this -

    ^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
    

    http://www.w3.org/TR/html5/forms.html#valid-e-mail-address

     ^ 
     [a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+ 
     @
     [a-zA-Z0-9] 
     (?:
          [a-zA-Z0-9-]{0,61} 
          [a-zA-Z0-9] 
     )?
     (?:
          \. 
          [a-zA-Z0-9] 
          (?:
               [a-zA-Z0-9-]{0,61} 
               [a-zA-Z0-9] 
          )?
     )*
     $ 
    

提交回复
热议问题