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