Skip to content

Instantly share code, notes, and snippets.

@dcoppari
Created January 29, 2024 15:44
Show Gist options
  • Save dcoppari/302658bdd7122ba24bee88939edbfdf1 to your computer and use it in GitHub Desktop.
Save dcoppari/302658bdd7122ba24bee88939edbfdf1 to your computer and use it in GitHub Desktop.
Validate email formart and domain
#!/usr/bin/env bash
function valid_format {
local email=$1
local 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,})+$"
[[ $email =~ ${regex} ]] || return 1
}
function valid_domain {
local fqdn=$1
[[ $fqdn == *"."* ]] || return 1
host $fqdn >/dev/null 2>&1 || return 1
}
email=$1
valid_format $email
if [[ $? == 0 ]]; then
domain="${email#*@}"
valid_domain $domain
if [[ $? == 0 ]]; then
echo "LEGIT!"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment