anixon 0 Posted February 1, 2007 I have written the following code to test that the email address input is valid. The routine forces an error if: - The address contains a space - The address does not contain an @ or there are more than 2 appearances of this character or the character appears as either the first or last character in the string - The address does not contain a "." [full stop] or the character appears as either the first or last character in the string but there can be as many appearances of it within the string My query is there a more efficient way of testing input against a mask? CODE$input = InputBox("Email", "Enter an Email Address.", "", "") $length = StringLen($input) $it = 0 $test = 0 For $it = $length to 0 Step - 1 If StringInStr($input, ".", 0, $it) = 1 or StringInStr($input, ".", 0, $it) = $length or StringInStr($input, ".", 0, 1) = 0 Then $test = 1 ExitLoop ElseIf StringInStr($input, "@", 0, 1) = 1 or StringInStr($input, "@", 0, 1) = 0 or StringInStr($input, "@", 0, $it) = $length Then $test = 1 ExitLoop Elseif StringInStr($input, "@", 0, $it) > StringInStr($input, "@", 0, 1) Then $test = 1 ExitLoop Elseif StringInStr($input, " ", 0, 1) > 0 Then $test = 1 ExitLoop EndIf Next If $test = 0 Then msgbox(0, "", "Correct Address" ,5) Else msgbox(0, "", "Incorrect Address" ,5) EndIf Cheers Ant.. Share this post Link to post Share on other sites
SmOke_N 207 Posted February 1, 2007 (edited) Func _IsVailidEmail($sEmail) If StringRegExp($sEmail, "^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|" & _ "((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$") Then Return 1 Return SetError(1, 0, 0) EndFunc Edited February 1, 2007 by SmOke_N Hide SmOke_N's signature Hide all signatures Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Share this post Link to post Share on other sites
anixon 0 Posted February 2, 2007 Func _IsVailidEmail($sEmail) If StringRegExp($sEmail, "^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|" & _ "((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$") Then Return 1 Return SetError(1, 0, 0) EndFunc That function works perfectly. I very much like the way that it also error traps the other characters not applicable to a valid email address. Thanks for the prompt reply which was very much appreciated. Cheers Ant.... Share this post Link to post Share on other sites