Sign in to follow this  
Followers 0
flyingboz

Isvalidsmtpaddress ?

6 posts in this topic

In the spirit that I posted the IsValidFilename a while back, I was wondering if anyone had already written a UDF to validate email address syntax, or has a sample in another language I might quickly/easily adapt to au3?

Thanks for looking.


Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this. It is untested, but I think it will work. :(

Edit: It should catch when sections are too short now. :ph34r:

Edit: Fix bugs (and more bugs)

Dim $NAME = InputBox("Title", "Please enter an e-mail address to check.")

If IsValidSmtpAddress($NAME) Then   
   MsgBox(0, "Testing", "Valid E-Mail address")
Else
   MsgBox(0, "Testing", "Not Valid")
EndIf

Func IsValidSmtpAddress($ADDRESS)
   Local $I, $M
   Local $NPOS, $NDPOS
   
  ;Valid format:  alpha.numeric02@whereever.com
   $NPOS = StringInStr($ADDRESS, "@")
   If $NPOS = 0 Then Return 0; Not found: False
   If $NPOS = 1 Then Return 0; No name: False
   For $I = 1 To $NPOS - 1
      $M = StringMid($ADDRESS, $I, 1)
      Select
         Case StringIsAlNum($M) ;Alphanumeric - valid
           ; Do nothing
         Case StringInStr(".%_-", $M) > 0
           ; Do nothing
         Case Else   
            Return 0;False
      EndSelect
   Next
   
  ; Valid name, now for the domain.
  ; Find the .com, .net, etc.
   $NDPOS = StringInStr($ADDRESS, ".", 0, -1)
   If $NDPOS < $NPOS Then Return 0 ; Not found - False
   If $NDPOS = StringLen($ADDRESS) Then Return 0  ; No Top-Level domain.  False
   If $NDPOS - $NPOS = 1 Then Return 0  ; No domain name. False
  ;scan all characters after @
   For $I = $NPOS + 1 To StringLen($ADDRESS)
      $M = StringMid($ADDRESS, $I, 1)
      Select
         Case StringIsAlNum($M);Alphanumeric - valid
           ; Do nothing
         Case StringInStr(".%_-", $M) > 0
           ; Do nothing
         Case Else   
            Return 0;False
      EndSelect
   Next
   Return 1;True
EndFunc  ;==>IsValidSmtpAddress
Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Thats neat . could someone add a input box to it so you can test diff numbers .

Share this post


Link to post
Share on other sites

Thanks, Nutster ,it looks great!-

Did you just whip that out on the fly? If so, you make me feel lazy. Of course, I use autoit, so.... :ph34r:

If you might indulge a programming question - (Please note that I am not CARPING or COMPLAINING!)

I noticed you used Dim in the function for local variable declaration rather than Local. Is this preferred, or could it lead to a potential problem? I thought using Local was safer (no chance of variable conflict), but if I'm wrong and misguided, please correct me.


Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Dim instead of Local? I was just being lazy while I whipped this off the top of my head :ph34r: . The code could still be a little buggy as I have only tested it with a few valid and invalid addresses.

That Dim should be Local, but it will have the same behaviour as Dim, unless there is a global variable of the same name!

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

That Dim should be Local, but it will have the same behaviour as Dim, unless there is a global variable of the same name!

<{POST_SNAPBACK}>

That's excatally why I never use Dim. I know it's eaisier if it's your first language, or first expierence in a language that uses function programming. However, comming from a slight C and Java background, I always perfer to state my scope. When playing with multipile scopes it's all too easy to accidentally re-use a global variable with a command like 'Dim.' And those kind of mistakes can be hard to catch when you go to look for it.

quote fix

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0