Skip to content

Instantly share code, notes, and snippets.

@guessi
Last active April 23, 2024 15:08
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=(
"v@example.com"
"v@e.com"
"v@e.me"
"valid@123.com"
"valid@example.com"
"valid+1@example.com"
"valid+1@ex.ample.com"
"valid+1@ex-ample.com"
"valid+1@ex_ample.com"
"valid+01@example.com"
"valid+01@ex.ample.com"
"valid+01@ex-ample.com"
"valid+01@ex_ample.com"
"vali.d@example.com"
"va-lid@example.com"
"v-a-l-i-d@example.com"
"v.a-lid@example.com"
"v.ali-d@example.com"
"v_ali-d@example.com"
"va_li-d@example.com"
"v_a_li_d@example.com"
"v.ali-d@example.com"
"v.ali.d@example.com"
"v0.ali.d@example.com"
"v0.ali.d1@example.com"
"valid@e-x_ample.com"
"valid@e.x-ample.c.om"
"valid+alias@example.com"
"valid+1+alias@example.com"
"valid+alias+user@example.com"
"valid+xyz+alias@example.com"
"valid01@example.com"
"valid-01@example.com"
"valid_01@example.com"
"valid.01@example.com"
"va-lid-0-1@example.com"
"va.lid-0-1@example.com"
"val-id.0-1@example.com"
"0.vali.d@example.com"
"0.vali.1@example.com"
"valid++alias@example.com"
"valid..alias@example.com"
"valid--alias@example.com"
"valid__alias@example.com"
"v.-alid@example.com"
"v-.-alid@example.com"
"vali-.-d@example.com"
"val..id@example.com"
"va--lid@example.com"
"val_-id@example.com"
"val-.id@example.com"
"valid..2020@example.com"
"valid__2020@example.com"
"valid+-2020@example.com"
)
invalid_inputs=(
"invalid-@example.com"
"invalid.@example.com"
"invalid_@example.com"
"invalid+@example.com"
"-invalid@example.com"
".invalid@example.com"
"_invalid@example.com"
"+invalid@example.com"
"inv alid@example.com"
"invalid@exa mple.com"
"invalid@e.xample.c.o"
"invalid@_"
"invalid@com"
"invalid@example.c-.om"
"invalid@example._com"
"invalid@e-x._ample.com"
"invalid@e-.x_ample.com"
"invalid@e.-x_ample.com"
"invalid@example.c"
"invalid@example._"
"invalid@example.-"
"invalid@example.+"
"invalid@exampl.0e"
"invalid@exampl.e1"
"invalid@exampl.1"
"invalid@example."
"invalid@example.com."
"+alias@example.com"
"invalid@-.com"
"invalid@_.com"
"invalid@.com"
"invalid@com"
"invalid@.com"
"invalid@+com"
"invalid@-com"
"invalid@_com"
"invalid@."
"invalid@_"
"invalid@-"
"invalid@...com"
"@"
".@."
"-@-"
"_@_"
"+@+"
"a.@.."
"@com"
"@example.com"
"_@example.com"
".@example.com"
"invalid@_123.com"
"invalid@example.123"
"invalid@example.123.456"
)
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 "_#@us@.com" ;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 "_#@us@.com" ;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 invalid@example.com.

@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 -
a@b.com
qwerty@b.com
a@qwerty.com

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 testuser-01@example.com . 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 valid+1@example.com

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 abc..2002@gmail.com

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 abc..2002@gmail.com 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 :-)
test@xn--db-pla.eu
test@sport.xn--dbrowa-w0a.pl
test@xn--niadek-2ib.pl

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