News for hackers. Stuff that matters.
Re:The Reason Java Is Still Popular by ipaddr on Thursday September 22, 2022 @02:45PM (#32943000)
How would you do it
Re:The Reason Java Is Still Popular by littlestymaar on Thursday September 22, 2022 @04:15PM (#32944051)
Two options:

- use a library that implements RFC 5322 properly.

- don't validate the email address (that is, don't try to reject most invalid addresses) and just check that the field contains at least one `@` to be sure the user didn't enter something else than his email address by accident.

Tying to perform email address validation with regex you're writing yourself is the best way to reject perfectly acceptable email address and get frustrated users.

Re:The Reason Java Is Still Popular by mrkeen on Thursday September 22, 2022 @04:55PM (#32944514)
Re:The Reason Java Is Still Popular by papercrane on Thursday September 22, 2022 @03:00PM (#32943187)
Check that its a string containing '@' with at least one character before and after the @ symbol is usually the only sensible thing to do.

The format for emails is surprisingly complex, and while you can use a regex to catch 99% of the right values, it's very difficult to get it 100% right. Often the best thing to do is just to try and send an email to the address a validation link.

Re:The Reason Java Is Still Popular by __ryan__ on Thursday September 22, 2022 @03:08PM (#32943303)

  Check that its a string containing '@' with at least one character before and after the @ symbol is usually the only sensible thing to do.
This is exactly what the regex in the example is doing.
Re:The Reason Java Is Still Popular by papercrane on Thursday September 22, 2022 @03:21PM (#32943457)
The regex is ^[^@\s]+@\S+$

This would reject valid email addresses using quoted parts like john."@"[email protected].

Edit: Looking at it closely it looks like the attempt is to reject anything with an '@' in the user part, which would be an error (e.g. john."@"[email protected]). Except since "\S+" also includes '@' this regex will still work, but by accident. It would reject the valid email john."\ "[email protected]

Re:The Reason Java Is Still Popular by __ryan__ on Thursday September 22, 2022 @03:29PM (#32943563)
I don't think that's true. In JS:

  /^[^@\s]+@\S+$/.test("john.\"@\"[email protected]") == true
Edit: Hmm, I see what you mean. At first glance I thought it was just checking that it didn't start with @ or whitespace.
Re:The Reason Java Is Still Popular by papercrane on Thursday September 22, 2022 @03:34PM (#32943616)
Sorry, I edited my comment and then noticed you replied already. It's working by accident since the '\S+' will match the second '@', but if you had a quoted space in the user it would not work e.g. john."\ "[email protected], but would work with [email protected]@invalid.example which I believe the regex is author meant to be caught as invalid.