4 ms·
I have such a routine for e-mail addresses: int is_valid_email(const char* email) { return strlen(email) > 0; } You're welcome. Coincidentally
by layoutIfNeeded 6y ago
I have such a routine for e-mail addresses:
int is_valid_email(const char* email) {
return strlen(email) > 0;
}
You're welcome. Coincidentally it also works for validating people's names and addresses too.
- zzo38computer 6y agoThat isn't very good. I would think better would be: int is_valid_email(const char*email) { return !!*email; } But, either way, it isn't good enough, because it is considered valid even if there is no at sign. With some applications, such as email on a local system, you don't need to worry about that, but on internet, you will need to check that there is a at sign in the email address. So, better would be to check if there is a at sign, and if so, then it is considered to be valid. Therefore, probably the best way is: int is_valid_email(const char*email) { return !!strchr(email,'@'); }
- saagarjha 6y agoI don't see the difference between the one that was linked and the first one you posted, other than the other one invoking undefined behavior if not passed a null-terminated string.
- zzo38computer 6y agoThe difference is that it is potentially more efficient (depending what the optimizer does). (At least, it is how I do C programming.)
- oxguy3 6y agoI think you could reasonably go a step further and require that there be at least one character before and after the at sign. And you could probably ban ASCII control characters from appearing anywhere in the address. Depending on your needs, you might even want to try resolving the hostname or connecting to the SMTP server. In a real-world application, you don't just want to check if an address meets an RFC; you want an address you can use throughout your application. Email addresses such as `h\@x0rz@!!!` or `\n@\0` or `@` might be RFC-compliant, but they are plainly ridiculous and would never be used by a real user. Any time/effort you spend writing tests or fixing bugs caused by permitting these esoteric addresses would be better spent on anything else.
- layoutIfNeeded 6y agoAny time/effort you spend writing tests or fixing bugs caused by permitting whitespaces in filenames would be better spent on anything else. Any time/effort you spend writing tests or fixing bugs caused by permitting Unicode characters in domain names would be better spent on anything else. Any time/effort you spend writing tests or fixing bugs caused by permitting leap seconds in timestamps would be better spent on anything else. Etc.