Skip to content

Instantly share code, notes, and snippets.

@guessi
Last active October 9, 2024 15:52
Show Gist options
  • Save guessi/82a73ee7eb2b1216eb9db17bb8d65dd1 to your computer and use it in GitHub Desktop.
Save guessi/82a73ee7eb2b1216eb9db17bb8d65dd1 to your computer and use it in GitHub Desktop.
Simple Email Validator in Bash
#!/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
@guessi
Copy link
Author

guessi commented Oct 3, 2020

@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
Copy link

phpmoli commented May 19, 2021

@guessi according to this, you can have a valid email address with multiple @ characters in it.

@guessi
Copy link
Author

guessi commented May 20, 2021

@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 👍

@takenek
Copy link

takenek commented May 20, 2022

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

@bharatbh0
Copy link

bharatbh0 commented Sep 22, 2023

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,})+$"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment