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

Great code! No need to echo and I would return the result :

function isEmailValid() {
      regex="^([A-Za-z]+[A-Za-z0-9]*((\.|\-|\_)?[A-Za-z]+[A-Za-z0-9]*){1,})@(([A-Za-z]+[A-Za-z0-9]*)+((\.|\-|\_)?([A-Za-z]+[A-Za-z0-9]*)+){1,})+\.([A-Za-z]{2,})+"
      [[ "${1}" =~ $regex ]]
}

if isEmailValid "_#@[email protected]" ;then
        echo "VALID "
else
        echo "INVALID"
fi

@dennis-michaelis
Copy link

Great code! No need to echo and I would return the result :

function isEmailValid() {
      regex="^([A-Za-z]+[A-Za-z0-9]*((\.|\-|\_)?[A-Za-z]+[A-Za-z0-9]*){1,})@(([A-Za-z]+[A-Za-z0-9]*)+((\.|\-|\_)?([A-Za-z]+[A-Za-z0-9]*)+){1,})+\.([A-Za-z]{2,})+"
      [[ "${1}" =~ $regex ]]
}

if isEmailValid "_#@[email protected]" ;then
        echo "VALID "
else
        echo "INVALID"
fi

Nice RegEx and short function to test! I extended it with a $ sign. Otherwise the function return "valid" to an mail address with a dot in the end.

function isEmailValid() {
      regex="^([A-Za-z]+[A-Za-z0-9]*((\.|\-|\_)?[A-Za-z]+[A-Za-z0-9]*){1,})@(([A-Za-z]+[A-Za-z0-9]*)+((\.|\-|\_)?([A-Za-z]+[A-Za-z0-9]*)+){1,})+\.([A-Za-z]{2,})+$"
      [[ "${1}" =~ $regex ]]
}

Now this mail ist INVALID [email protected].

@guessi
Copy link
Author

guessi commented Aug 4, 2019

@nycmitch25
Thanks

@dennis-michaelis

Thanks for reporting. I just fixed that issue, and simplified the script.

@Biep
Copy link

Biep commented Apr 8, 2020

I had hoped to find a true validator - along these lines. If you ever want to go that way, here is a start.

@anujatupe
Copy link

anujatupe commented Apr 10, 2020

This regex is failing for the following email addresses which are VALID email addresses -
[email protected]
[email protected]
[email protected]

Any of the below regex works fine for all the test cases mentioned in your sh file as well as the email addresses I mentioned above -
^([A-Za-z]+[A-Za-z0-9]*((\.|\-|\_)?[A-Za-z]+[A-Za-z0-9]*){0,})@(([A-Za-z]+[A-Za-z0-9]*)+((\.|\-|\_)?([A-Za-z]+[A-Za-z0-9]*)+){0,})+\.([A-Za-z]{2,})+$
OR
^([A-Za-z]+[A-Za-z0-9]*((\.|\-|\_)?[A-Za-z]+[A-Za-z0-9]*)*)@(([A-Za-z]+[A-Za-z0-9]*)+((\.|\-|\_)?([A-Za-z]+[A-Za-z0-9]*)+)*)+\.([A-Za-z]{2,})+$

@guessi
Copy link
Author

guessi commented Apr 10, 2020

@anujatupe thanks

I've fixed those problems and added more test cases, please have a look at the updated version.

@guessi
Copy link
Author

guessi commented Apr 10, 2020

@Biep indeed, having validation for the MX records would be better. Even more with NS records check.
But the script here is focus on the format, but not availability.

@dnicolaev
Copy link

dnicolaev commented Sep 29, 2020

the code is failing on [email protected] . Looks like it is failing on a dash followed by number -01.

@guessi
Copy link
Author

guessi commented Sep 29, 2020

@dnicolaev thanks for report. I've update the script and add more test cases.

@dnicolaev
Copy link

let's work again ;-)
now it is failing on this [email protected]

ps: Thank you for doing this. I really appreciate this tool.

@guessi
Copy link
Author

guessi commented Oct 1, 2020

@dnicolaev big thanks for hunting bugs in this script.

I've do some research for the RFC standards, and script updated.
please have a look for the latest script again.

@suraj9741
Copy link

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

@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