mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
Hi all,

What is the best way to check if an email address is in the correct format? either using a regex or any other method?

Thanks in advance to any replies.


Newbie
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
If you know you're looking at an email address and not something else, then a wildcard match using *@*.* would work, but I'm sure there's a better regex method. And if you don't know if it's an email address, then that would match things like an $address() (nick!ident@host.com), which you wouldn't want.


Invision Support
#Invision on irc.irchighway.net
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
While regex probably is the best method, there is no best expression... smile
Should give you an idea:
http://www.regular-expressions.info/email.html
http://regexlib.com/Search.aspx?k=&c=1&m=-1&ps=50

Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
I am unfamiliar with regex, so had alot of trouble following that article. I don't need it to be amazing or even foolproof. I just want a simple check on a string to see if it follows the basic format of an email address.


Newbie
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
I took the second expression from the first link, added separators and "case insensitive" flag, and set it into a custom identifier:
Code:
alias ismail {
  var %r = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  if ($regex($1,%r)) { return $true }
  return $false
}

Now try
Code:
//echo -a $ismail(test) $ismail(@nothing) $ismail(123@456) $ismail(nick!ident@host.domain) $ismail(Test@something.com)


And use $ismail(<address>), like in
Code:
alias mailtest {
  if ($ismail($input(Enter eMail address:,eog,Mail Address))) { echo -a valid address given: $! }
  else { echo -a no/invalid address given }
}


Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
Thank You. Very helpful


Newbie

Link Copied to Clipboard