Jump to content

To check user entering email address


Recommended Posts

How can I tell if a user entered email address is incorrect

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 448, 242)
$Input1 = GUICtrlCreateInput("Input1", 40, 40, 361, 21)
$Button1 = GUICtrlCreateButton("Button1", 184, 96, 75, 25)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $aReadMail = GUICtrlRead($Input1)
                    For $j = 0 To UBound($aReadMail) - 1
                        if stringregexp($aReadMail[$j],'^[a-zA-Z0-9-_\.]+\@[a-zA-Z0-9-_\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)\s\([A-Z][a-z]+\s[A-Z][a-z]+\)$') = 1 then
                     MsgBox(0,"Ok","Ok this is a true mail")
                        Else
                         MsgBox(0,"Error","This is not an email!")
                        EndIf
                    next
    EndSwitch
WEnd

 

Link to comment
Share on other sites

Try here

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I checked and unfortunately there was not a turn :(

http://rubular.com/r/OiGTQrrtFY

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StringConstants.au3>
$Form1 = GUICreate("Form1", 448, 242)
$Input1 = GUICtrlCreateInput("testmymailyoutuber@test.com", 40, 40, 361, 21)
$Button1 = GUICtrlCreateButton("Button1", 184, 96, 75, 25)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $aReadMail = GUICtrlRead($Input1)
                    ;For $j = 0 To UBound($aReadMail) - 1
                    ;For $j = 0 To UBound($aReadMail[0])
                        For $j = 1 To UBound($aReadMail) - 1
        ;if stringregexp($aReadMail[$j],'^[a-zA-Z0-9-_\.]+\@[a-zA-Z0-9-_\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)\s\([A-Z][a-z]+\s[A-Z][a-z]+\)$') = 1 then
         if stringregexp($aReadMail[$j],'^[a-zA-Z0-9-_\.]+\@[a-zA-Z0-9-_\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)') = 1 then
             ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aReadMail[$j] = ' & $aReadMail[$j] & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
                     MsgBox(0,"Ok","Ok this is a true mail")
                        Else
                         MsgBox(0,"Error","This is not an email!")
                        EndIf
                    next
    EndSwitch
WEnd

 

Edited by youtuber
Link to comment
Share on other sites

Hi,

check regex for autoit with regex101.com. Example here: https://regex101.com/r/8GPLJU/1

There are different regex syntaxes in different languages. You tried ruby but should use pcre(php).

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

By the way I use "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}". Because the top level domain should be between 2 and 63 characters.

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Thanks, how can I give an error message if this is not an email?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
$Form1 = GUICreate("Form1", 448, 242)
$Input1 = GUICtrlCreateInput("testmymailyoutuber@test.com", 40, 40, 361, 21)
$Button1 = GUICtrlCreateButton("Button1", 184, 96, 75, 25)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $aReadMail = GUICtrlRead($Input1)
$aRegex = StringRegExp($aReadMail, '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}', 3)
for $i=0 to ubound($aRegex)-1
    If IsArray($aRegex) Then
        MsgBox(0,"Ok",$aRegex[$i])
    Else
    MsgBox(0,"Error","This is not an email!")
EndIf
Next
    EndSwitch
WEnd

 

Link to comment
Share on other sites

Thanks @mikell Should it be this way?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
$Form1 = GUICreate("Form1", 448, 242)
$Input1 = GUICtrlCreateInput("testmymailyoutuber@test.com", 40, 40, 361, 21)
$Button1 = GUICtrlCreateButton("Button1", 184, 96, 75, 25)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $aReadMail = GUICtrlRead($Input1)
$aRegex = StringRegExp($aReadMail, '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$', 3)
If IsArray($aRegex) Then
    MsgBox(0,"Ok",$aRegex[0])
Else
    MsgBox(0,"Error","This is not an email!")
EndIf
    EndSwitch
WEnd

 

Link to comment
Share on other sites

^ at the beginning and $ at the end proves that the whole string is matching?

What is possible if I want to allow trailing spaces that I will strip later on (for user comfort reason)? Or should I kill all trailing spaces first and regex later?

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

 

1 hour ago, Simpel said:

^ at the beginning and $ at the end proves that the whole string is matching?

Yes

You can allow trailing spaces in the regex by adding \h* just before the $ and then remove them later if a clean address is needed in the script, or remove them before the check .... both ways are correct, choose your flavour  :)

Link to comment
Share on other sites

That is my whole snippet for provement:

#include <StringConstants.au3>
Local $sText = 'a@b.cd'
ConsoleWrite($sText & ": " & _ProveMailAdress($sText) & @CRLF)

Func _ProveMailAdress($sAdress)
    Local $sPattern = "^[A-Za-z0-9.!#$%&'*+\-\/=?^_`\{\|\}~]+@[a-zA-Z0-9.-]*?\.[a-zA-Z0-9.-]{2,63}$"
    If StringLeft($sAdress, 1) = "." Then Return 0 ; no dot at start
    If StringRight($sAdress, 1) = "." Then Return 0 ; no dot at end
    StringRegExp($sAdress, $sPattern, $STR_REGEXPARRAYMATCH)
    If @error Then Return 0
    Return 1
EndFunc

As you can see the local part is allowed to include some more symbols.

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...