youtuber Posted August 24, 2017 Share Posted August 24, 2017 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 More sharing options...
BrewManNH Posted August 24, 2017 Share Posted August 24, 2017 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 GudeHow 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 More sharing options...
youtuber Posted August 24, 2017 Author Share Posted August 24, 2017 (edited) 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 August 24, 2017 by youtuber Link to comment Share on other sites More sharing options...
Simpel Posted August 24, 2017 Share Posted August 24, 2017 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 youtuber 1 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) 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 More sharing options...
Simpel Posted August 24, 2017 Share Posted August 24, 2017 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 youtuber 1 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) 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 More sharing options...
youtuber Posted August 24, 2017 Author Share Posted August 24, 2017 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 More sharing options...
mikell Posted August 25, 2017 Share Posted August 25, 2017 You check one address, so no need of a For loop $aRegex = StringRegExp($aReadMail, ..., 3) If IsArray($aRegex) Then MsgBox(0,"Ok",$aRegex[0]) Else MsgBox(0,"Error","This is not an email!") EndIf BTW I suggest to add the "^" (start) and "$" (end) anchors in the regex pattern Link to comment Share on other sites More sharing options...
youtuber Posted August 25, 2017 Author Share Posted August 25, 2017 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 More sharing options...
mikell Posted August 25, 2017 Share Posted August 25, 2017 24 minutes ago, youtuber said: Should it be this way? youtuber 1 Link to comment Share on other sites More sharing options...
Simpel Posted August 25, 2017 Share Posted August 25, 2017 ^ 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) 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 More sharing options...
mikell Posted August 25, 2017 Share Posted August 25, 2017 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 youtuber 1 Link to comment Share on other sites More sharing options...
Simpel Posted August 25, 2017 Share Posted August 25, 2017 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 t0nZ 1 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) 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now