this was a fun one to make. Lots of conditions to check. I am not sure if there is an easier way to do it, but this works and checks all the criteria you mentioned plus I added checking for spaces.
'==========================================================================
'
' NAME: ValidateEmail.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL:
' DATE : 3/16/2004
'
' COMMENT: <comment>
'
'==========================================================================
email = InputBox("Enter Email Address","Email Address?")
email = UCase(email)
addrLength = Len(email)
failureReport = "Test Results: Valid Email Address"
If Asc(Right(email,1)) >=64 And Asc(Right(email,1))< 91 Then
Report = "Starts with an Alpha" & vbCrLf
Else
Report = "Does NOT start with an Alpha" & vbCrLf
failureReport = "Test Results: Invalid Email Address...Start Alpha"
End If
For x = 1 To addrLength
If Mid(email,x,1) = "@" Then
Report = Report & "Got an @ sign" & vbCrLf
atposition = x
End If
Next
If atposition > Int(addrLength) Then
Report = Report & "No @ sign" & vbCrLf
failureReport = "Test Results: Invalid Email Address...@"
End If
For x = 1 To addrLength
If Asc(Mid(email,x,1)) = 32 Then
spaces = "True"
Else
spaces = "False"
End If
Next
If spaces = "True" Then
Report = Report & "Space detected but not allowed"
failureReport = "Test Results: Invalid Email Address...Spaces"
Else
Report = Report & "No spaces detected" & vbCrlf
End If
If Asc(Right(email,4)) = 46 Then
Report = Report & "Got a period where it belongs" & vbCrLf
Else
Report = Report & "Does NOT have a period where it belongs" & vbCrLf
failureReport = "Test Results: Invalid Email Address...Period"
End If
RightStart = addrLength - 3
endAlpha = "True"
For y = 1 To 3
If Not Asc(Mid(email,(RightStart + y),1)) >=64 And Asc(Mid(email,(RightStart + y),1))< 91 Then
endAlpha = "False"
End If
Next
If endAlpha = "True" Then
Report = Report & "All alpha at the end" & vbCrLf
Else
Report = Report & "NOT all alpha at the end" & vbCrLf
failureReport = "Test Results: Invalid Email Address...End Alpha"
End If
WScript.Echo Report & vbCrLf & failureReport
I hope you find this post helpful. Please let me know if it was.
Regards,
Mark