Register
It is currently Sat Aug 02, 2014 2:35 am

If statement to force e-mail address in being entered


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Wed Jun 29, 2011 10:58 am   

Joined: Mon Apr 27, 2009 2:24 pm
Posts: 10
I have a useradd bash script which requests the user enter an e-mail address for the user being created. This is so the user receives his username/password in an e-mail when his/her account is created.

Currently this part of the code is very simple:

echo Enter the users e-mail address
read ADDRESS

What i'm finding is that sometimes when the operators run the script they are entereing blank information. How can I put a if statement in place that enforces they enter an e-mail address format.

I tried the following code but it doesn't work. The idea was to at least verify they are using the @ symbol.

string=@
if [[ $string != "@" ]] ; then
echo You have entered an invalid e-mail address!
exit 1
else
do something


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 2:53 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 534
Hi,

Code:
if [[ $emailAddress != [a-zA-Z0-9]*@[a-zA-Z0-9]* ]] ; then


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 7:55 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
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...


Top
 Profile  
 PostPosted: Sat Jul 09, 2011 8:27 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 664
Location: Des Moines, Iowa
Interesting link on validating emails with regex: http://www.regular-expressions.info/email.html

Code:
if [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ ]]
then
    echo "Email address $email is valid."
else
    echo "Email address $email is invalid."
fi


That would validate about 99% of valid emails in use today as valid.


Top
 Profile WWW  
 PostPosted: Sat Jul 09, 2011 11:26 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Also, check out those links:
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
http://haacked.com/archive/2007/08/21/i ... til-i.aspx

@crouse:
Don't quote the regex or it'll be evaluated as a string. And it's better to use a variable ( e.g. « if [[ $email =~ $re ]]; then » ).


Top
 Profile  
 PostPosted: Sun Jul 10, 2011 10:55 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 664
Location: Des Moines, Iowa
You are correct, I removed the quotes... ty.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP