mIRC Home    About    Download    Register    News    Help

Print Thread
email validator - regex or other method #224358 09/08/10 10:51 AM
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
Re: email validator - regex or other method [Re: HaleyJ] #224360 09/08/10 11:49 AM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
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
Re: email validator - regex or other method [Re: HaleyJ] #224361 09/08/10 01:15 PM
Joined: Nov 2006
Posts: 1,559
H
Horstl Offline
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

Re: email validator - regex or other method [Re: Horstl] #224367 09/08/10 02:15 PM
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
Re: email validator - regex or other method [Re: HaleyJ] #224370 09/08/10 02:29 PM
Joined: Nov 2006
Posts: 1,559
H
Horstl Offline
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 }
}


Re: email validator - regex or other method [Re: Horstl] #224374 09/08/10 03:13 PM
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