Sign in to follow this  
Followers 0
anixon

Input Mask

3 posts in this topic

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



#2 ·  Posted (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 by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

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

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