programming-examples/asp/Miscellaneous/Better Email and Domain Validation.asp
2019-11-18 14:25:58 +01:00

75 lines
3.1 KiB
Plaintext

Function ValidateEmailAddress(sEmailAddr)
Dim sDomain 'as String
'Get domain portion of email address.
sDomain = Mid(sEmailAddr, Instr(1, sEmailAddr, "@") + 1)
'Create Object for Reg. Expressions matching to validate address.
'This script will first validate that the email address is of valid format
'It then validates the domain portion by doing a nslookup.
Set oRegExp = new RegExp
oRegExp.Pattern = "^\w+(\.\w+)*@\w+\.\w+(\.\w+)*$"
oRegExp.IgnoreCase = true
'Excute the pattern matching to return a collection.
Set colMatches = oRegExp.Execute(sEmailaddr)
'If colMatches = 1 then the email address is of valid format.
Response.write "colMatch = " & colMatches.count & "<BR>"
'Response.write "colMatch = " & colMatches(0). & "<BR>"
If colMatches.Count = 1 then
'If the email address is valid format then we don't
'want to run this and create any more delay or cpu cycles
'than are needed.
If DomainLookup(sDomain) then ValidateEmailAddress = true
Else
'It wasn't Valid
ValidateEmailAddress = false
End If
Set colMatches = Nothing
Set objRegExpr = Nothing
End Function
Function DomainLookup(sDomain)
Dim sReadData 'As String
Dim fIpIsNextLine 'Ad boolean
'Create Shell Object
set oShell = Server.CreateObject("Wscript.Shell")
'Run NSLookup via Command Prompt
'Dump Results into a temp text file
'The text file is unique and stored in the directory specified
'using the %Tmp% environment variable. This file will be deleted
'after validation.
'If you can not validate against the local name server for some reason you can specify a
'Name server by uncommenting this line and replacing the name of the server.
'oShell.Run "%ComSpec% /c nslookup " & sDomain & " WEBTERMINATOR1.Crystaltech.com > %Tmp%" & _
' Session.SessionID & sDomain & ".txt", 0, True
oShell.Run "%ComSpec% /c nslookup " & sDomain & " > %Tmp%" & _
Session.SessionID & sDomain & ".txt", 0, True
'Open the temp Text File and Read out the Data
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set oText = Fso.OpenTextFile("%Tmp%" & Session.SessionID & sDomain & ".txt")
Do While Not oText.AtEndOfStream
'Read In the Text Dump
sReadData = Trim(oText.Readline)
'If the domain name was found in the previous line read then this should be the IP.
If fIpIsNextLine then DomainLookup = True
'If the domain name was found in the Read line then the tell it the
'next line is the Ip. If an IP address was not found then it should not
'return the domain we are looking for in the txt file.
Response.write sReadData & "<BR>"
If Instr(1, sReadData, sDomain) then fIpIsNextLine = true
Loop
'Close it
oText.Close
'Delete It
FSO.DeleteFile "%Tmp%" & Session.SessionID & sDomain & ".txt"
Set FSO = Nothing
End Function