Actually, an e-mail address can contains other characters than letters and numbers (according to
RFC 2822,
RFC 5321, and
RFC 5322) and there are a bunch of restrictions. It'd therefore be very complicated to provide a
good and
efficient regular expression to validate one.
Anyway, what you want to do is checking whether the string entered contains a
@ symbol, and your approach was correct, except that you forgot to put globs around the expression.
Code:
read -rp "Enter user's e-mail address: "
if [[ $REPLY != *@* ]]; then
echo 'You have entered an invalid e-mail address!' >&2
exit 1
fi
# the e-mail address is valid, go ahead...
Also, you can re-prompt:
Code:
while read -rp "Enter user's e-mail address: "; [[ $REPLY != *@* ]]; do
:
done
# the e-mail address is valid, go ahead...