-
-
Save tediscript/63a7b99ee6de3fce0401788e0bae75d8 to your computer and use it in GitHub Desktop.
Simple Email Validator in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
validator() { | |
printf "* %-48s" "$1" | |
echo "$1" | egrep --quiet "^([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,})+" | |
[ $? -eq 0 ] && printf "\e[1;32m[pass]\e[m" || printf "\e[1;31m[fail]\e[m" | |
echo | |
} | |
echo | |
echo "a simple email validator in shell" | |
echo | |
echo "online validator: http://emailregex.com/" | |
echo "RFC: http://emailregex.com/email-validation-summary/" | |
echo | |
echo | |
echo "### expected result: valid" | |
echo | |
validator "valid@example.com" | |
validator "vali.d@example.com" | |
validator "va-lid@example.com" | |
validator "v-a-l-i-d@example.com" | |
validator "v.a-lid@example.com" | |
validator "v.ali-d@example.com" | |
validator "v_ali-d@example.com" | |
validator "va_li-d@example.com" | |
validator "v_a_li_d@example.com" | |
validator "v.ali-d@example.com" | |
validator "v.ali.d@example.com" | |
validator "v0.ali.d@example.com" | |
validator "v0.ali.d1@example.com" | |
validator "valid@e-x_ample.com" | |
validator "valid@e.x-ample.c.om" | |
validator "valid@e.xample.c.a.tw" | |
echo | |
echo "### expected result: invalid" | |
echo | |
validator "-invalid@example.com" | |
validator ".invalid@example.com" | |
validator "_invalid@example.com" | |
validator "inv alid@example.com" | |
validator "invalid@exa mple.com" | |
validator "inv.-alid@example.com" | |
validator "inv-.-alid@example.com" | |
validator "invali-.-d@example.com" | |
validator "inval..id@example.com" | |
validator "inva--lid@example.com" | |
validator "inval_-id@example.com" | |
validator "inval-.id@example.com" | |
validator "invalid.@example.com" | |
validator "invalid_@example.com" | |
validator "invalid-@example.com" | |
validator "invalid.0@example.com" | |
validator "invalid.1@example.com" | |
validator "0.invali.d@example.com" | |
validator "0.invali.1@example.com" | |
validator "inv.ali.0d@example.com" | |
validator "invalid@example.c-.om" | |
validator "invalid@example._com" | |
validator "invalid@e-x._ample.com" | |
validator "invalid@e-.x_ample.com" | |
validator "invalid@e.-x_ample.com" | |
validator "invalid@example.c" | |
validator "invalid@example._" | |
validator "invalid@exampl.0e" | |
validator "invalid@exampl.e1" | |
validator "invalid@exampl.1" | |
validator "invalid@exampl.1a.b.c" | |
echo | |
echo "### END" | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment