Here’s my address: @@@@@
The only correct regex for email is: .+@.+
So long as the address has a local part, the at sign, and a hostname, it’s a valid email address.
Whether it goes somewhere is the tricky part.
tomaThomas@lemmy.world 1 year ago
laurelraven@lemmy.blahaj.zone 1 year ago
And this right here is a great example of why simple basic RegEx is rarely adequate
At the very least, should be something like
^[^@\s]+@([^@\s.]+\.)+[^@\s.]+$
I’m like 99% sure I missed at least a few cases there, and will say “please don’t use this for anything production”
jpeps@lemmy.world 1 year ago
Here’s two: you can have multiple @s forming relays in an email address, and you can also break all the rules around dots and spaces if you put quotes around the local part, eg “.sarah…”@emails.com
laurelraven@lemmy.blahaj.zone 1 year ago
And this is exactly why I wouldn’t do my own, I had no idea either of those were legal/possible
xantoxis@lemmy.world 1 year ago
Sorry, this is not a correct regex for an email address.
Sending using ‘mail’ on a local unix system? You only need the local part.
STOP VALIDATING NAMES AND EMAIL ADDRESSES. Send a verification email. Full stop. Don’t do anything else. You really want to do this anyway, because it’s a defense against spam.
stardreamer@lemmy.blahaj.zone 1 year ago
*Gasp* the registration is coming from inside the data center!
Turun@feddit.de 1 year ago
I think it’s fair to prevent users from causing mail sent to your internal systems. It probably won’t cause any issues getting mail to the machine inbox for (no domain name), but it reasonably makes security uneasy.
xantoxis@lemmy.world 1 year ago
The statement I was responding to was “This is the correct email regex”. There is no correct email regex. Don’t parse emails with a regex. You probably don’t need to parse emails at all.
elrik@lemmy.world 1 year ago
Yes, but no. Pretty much every application that accepts an email address on a form is going to turn around and make an API call to send that email. Guess what that API is going to do when you send it a string for a recipient address without an @ sign? It’s going to refuse it with an error.
Therefore the correct amount of validation is that which satisfies whatever format the underlying API requires.
For example, AWS SES requires addresses in the form UserName@[SubDomain.]Domain.TopLevelDomain along with other caveats. If the application is using SES to send emails, I’m not going to allow an input that doesn’t meet those requirements.
xantoxis@lemmy.world 1 year ago
You mean the validation which the underlying API will perform on its own? You don’t need to do it.
elrik@lemmy.world 1 year ago
I disagree. You should have validation at each layer, as it’s easier to handle bad inputs and errors the earlier they are caught.
It’s especially important in this case with email because often one or more of the following comes into play when you’re dealing with an email input:
I’m not suggesting that validation of an email should attempt to be exhaustive, but a well thought-out implementation validates all user inputs. Even the underlying API in this example is validating the email you give it before trying to send an email through its own underlying API.
Passing obvious garbage inputs down is just bad practice.