-
-
Save guessi/82a73ee7eb2b1216eb9db17bb8d65dd1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash | |
# | |
# RFC standard of the email address could be found in rfc5322, rfc6854, etc. | |
# | |
# however, it is hard to escape all the special characters in the bash | |
# so, in this script, it will only check for the address syntax only | |
# having some valid/invalid inputs to test its result | |
# | |
# please be noted that, it is not design to detect mailbox deliverability | |
# | |
regex="^(([A-Za-z0-9]+((\.|\-|\_|\+)?[A-Za-z0-9]?)*[A-Za-z0-9]+)|[A-Za-z0-9]+)@(([A-Za-z0-9]+)+((\.|\-|\_)?([A-Za-z0-9]+)+)*)+\.([A-Za-z]{2,})+$" | |
valid_inputs=( | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"valid+1@ex_ample.com" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"valid+01@ex_ample.com" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"valid@e-x_ample.com" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
) | |
invalid_inputs=( | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"inv [email protected]" | |
"invalid@exa mple.com" | |
"[email protected]" | |
"invalid@_" | |
"invalid@com" | |
"[email protected]" | |
"invalid@example._com" | |
"invalid@e-x._ample.com" | |
"[email protected]_ample.com" | |
"[email protected]_ample.com" | |
"[email protected]" | |
"invalid@example._" | |
"[email protected]" | |
"invalid@example.+" | |
"[email protected]" | |
"[email protected]" | |
"[email protected]" | |
"invalid@example." | |
"[email protected]." | |
"[email protected]" | |
"[email protected]" | |
"invalid@_.com" | |
"[email protected]" | |
"invalid@com" | |
"[email protected]" | |
"invalid@+com" | |
"invalid@-com" | |
"invalid@_com" | |
"invalid@." | |
"invalid@_" | |
"invalid@-" | |
"[email protected]" | |
"@" | |
".@." | |
"-@-" | |
"_@_" | |
"+@+" | |
"a.@.." | |
"@com" | |
"@example.com" | |
"[email protected]" | |
"[email protected]" | |
"invalid@_123.com" | |
"[email protected]" | |
"[email protected]" | |
) | |
function validator { | |
if [[ $1 =~ ${regex} ]]; then | |
printf "* %-48s \e[1;32m[pass]\e[m\n" "${1}" | |
else | |
printf "* %-48s \e[1;31m[fail]\e[m\n" "${1}" | |
fi | |
} | |
cat <<-EOF | |
a simple email address validator in bash | |
online validator: http://emailregex.com/ | |
RFC Standards for email address | |
- https://tools.ietf.org/html/rfc821 | |
- https://tools.ietf.org/html/rfc5322 | |
- https://tools.ietf.org/html/rfc6854 | |
- https://tools.ietf.org/html/rfc5321 | |
- https://tools.ietf.org/html/rfc5322#section-3.4 | |
References: | |
- http://emailregex.com/email-validation-summary/ | |
EOF | |
echo | |
echo "### expected result: valid" | |
echo | |
for input in "${valid_inputs[@]}"; do | |
validator "${input}" | |
done | |
echo | |
echo "### expected result: invalid" | |
echo | |
for input in "${invalid_inputs[@]}"; do | |
validator "${input}" | |
done |
@suraj9741 I've made some fixes for the validation rules according to the RFC standards.
but the address [email protected]
is actually a valid address,
please have a look at the RFC standard definition. (RFC-5322, section 3.4 and section 3.4.1).
as for the part of password validation,
I think there's no validator available out there, password could in any character, in any format, isn't it?
@phpmoli interesting fact, but I'd prefer not to change this script for now. for the normal use case, we don't use multiple "@" in email address?
anyway, thanks for let me know 👍
Hey guys this is nice code :-) i find another bug :-)
He report this is wrong e-mail :-)
[email protected]
[email protected]
[email protected]
Best Regards
TaKeN
invalid@user_in_domain.com is showing up as valid.
this fixed the problem:
regex="^(([A-Za-z0-9]+((.|-|_|+)?[A-Za-z0-9]?)[A-Za-z0-9]+)|[A-Za-z0-9]+)@(([A-Za-z0-9]+)+((.|-)?([A-Za-z0-9]+)+))+.([A-Za-z]{2,})+$"
the code is failing on this [email protected]
thanks this is too good it's very helpful
I have a question on a different problem for validation fo password
you have any pattern for that