One way to do this is to use a command grep, for example:
grep -qv "[^0-9a-z-]" <<< $STRING
Then you request a return value grepwith the following:
if [ ! $? -eq 0 ]; then
echo "Wrong string"
exit 1
fi
As @mpapis pointed out, you can simplify the above expression:
grep -qv "[^0-9a-z-]" <<< $STRING || exit 1
You can also use the bash operator =~, for example:
if [[ ! "$STRING" =~ [^0-9a-z-] ]] ; then
echo "Valid";
else
echo "Not valid";
fi