Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple validation script

Status
Not open for further replies.

eavan

Technical User
Aug 6, 2003
51
GB
hi all,

new to vb script...basically just trying to validate details from a form....trying to ensure that what is submitted through a particular field is an actual email address..beginning with letters,contains one @ sign,continues with more letters,a full stop and then finishes with 2 or 3 letters...if anyone has a function that would do this i would really appreciate it.Thanks again
 
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
 
cheers Mark,

that has a few other things i didn't think about as well.Appreciate it.
 
My pleasure.

You probably already figured it out, but you can get a simple pass fail report by just changing the line:

WScript.Echo Report & vbCrLf & failureReport

To read:

WScript.Echo failureReport



I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hi Mark,

Thanks for getting back to me again.I didn't use the last post yet but would you have any code for a function that takes an arg submitted from another form field, and corrects it to ensure it is a valid web url...the first 7 char have to be " prob if you don't don't.Can't figure out which method to use for url encoding.Cheers
 
You could do checking like this:

getURL = InputBox("EnterURL","What URL?")

If lcase(Left(getURL,7)) <> " Then
WScript.Echo("Not a valid URL")
End IF

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
thanks again Mark...hopefully ill be alot more up to date with scripting soon enough...just getting into it.Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top