Hello Everyone,
I have the following script that should check both name and email for the appropriate characteristics, and return appropriate error messages, depending on the error. When all is successful, it submits to a DB.
The problems that I can not seem to work out, are:
1 - I need the correct messages to appear (see errorArray) upon a problem.
2 - The correct field name must be highlighted red when there is a problem. currently, only the "email:" field title highlights, even when there is a prob with the name field.
also, upon success, the field names must return to the black color.
I have not been able to figure this one out, and any help would be appreciated.
Gary
I have the following script that should check both name and email for the appropriate characteristics, and return appropriate error messages, depending on the error. When all is successful, it submits to a DB.
The problems that I can not seem to work out, are:
1 - I need the correct messages to appear (see errorArray) upon a problem.
2 - The correct field name must be highlighted red when there is a problem. currently, only the "email:" field title highlights, even when there is a prob with the name field.
also, upon success, the field names must return to the black color.
I have not been able to figure this one out, and any help would be appreciated.
Code:
<%@ Language=VBScript %>
<!-- #INCLUDE FILE = "DataStore.inc" -->
<!-- #INCLUDE FILE = "adovbs.inc" -->
<%DIM ErrorMsg,xemail,xname%>
<%
'Error Messages
Dim errorArray(4)
errorArray(0) = "Record Updated Successfully"
errorArray(1) = "Error In User Name"
errorArray(2) = "Error In E-mail Address"
errorArray(3) = "Please fix both your name and email"
'get the error code from the url
errorCode = Request.QueryString("error")
'If submit, then update DB
if request.form("isSubmitted") = "yes" then
'Format Values
xname = REPLACE(trim(request("xname")),"'","''")
xemail = REPLACE(trim(request("xemail")),"'","''")
'Check fields for valid characters
dim re
set re = New RegExp
re.Global = True
re.IgnoreCase = True
'Name
re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$"
If re.Test(xname)=false Then
Response.redirect "aspvalid4.asp?error=1&uname=" & xname & "&uemail=" & xemail
Response.end
end if
'Email
re.Pattern = "^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
If re.Test(xemail)=false then
Response.redirect "aspvalid4.asp?error=2&uname=" & xname & "&uemail=" & xemail
end if
'=====================================TLD TEST CODE
Dim emailArray
Dim TLDisValid
Dim ValidTLD(4)'modify this
TLDisValid=false
ValidTLD(0) = "com"
ValidTLD(1) = "us"
ValidTLD(2) = "net"
ValidTLD(3) = "ca"
emailArray = split(xemail,".")
For i = 0 to ubound(ValidTLD)
If lcase(emailArray(ubound(emailArray))) = lcase(ValidTLD(i)) then
TLDisValid=True
end if
next
If TLDisValid=False then
Response.redirect "aspvalid4.asp?error=2&uname=" & xname & "&uemail=" & xemail
Response.end
end if
'========================================END TLD TEST CODE
'create variable to hold database object and initialize it
Dim objRec
Set objRec = Server.CreateObject("ADODB.Recordset")
'open the database table asptest
objRec.Open "test", strConnect, adOpenStatic, _
adLockOptimistic, adCmdTable
'create a new record
objRec.AddNew
'set the field equal to the variable
objRec("EMail") = xemail
objRec("Name") = xname
'update the database with the new record
objRec.Update
'close the database
objRec.Close
Set objRec = Nothing
'Return Success
Response.redirect "aspvalid4.asp?error=0&uname=&uemail="
Response.end
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>ASP Form Validation Sample</title>
<style type="text/css">
<!--
body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
table { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
-->
</style>
</head>
<body bgcolor="#FFFFFF">
<%'Display Error
If len(errorCode)>0 then%>
<font color="red" size="3"><%=errorArray(errorCode)%></font>
<%end if 'Display Error%>
<form name="sample1" method="post">
<table width="50%" cellspacing="1" cellpadding="0" border="0">
<tr>
<td>
<% If len(errorCode)=2 then %><font color="red"><b><% end if %>Name: <% If len(errorCode)=2 then %></b></font><% end if %>
</td>
<td>
<input name="xname" value="<%=Request.QueryString("uname")%>" size="20">
</td>
</tr>
<tr>
<td>
<% If len(errorCode)=1 then %><font color="red"><b><% end if %>Email Address: <% If len(errorCode)=1 then %></b></font><% end if %>
</td>
<td>
<input name="xemail" value="<%=Request.QueryString("uemail")%>" size="20">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"><input type="hidden" name="isSubmitted" value="yes">
</td>
</tr>
</table>
</form>
<A href="[URL unfurl="true"]http://www.robarspages.ca/devroot/compUTERsite/list.asp">Check[/URL] the DB by clicking here.</A><br><br>
</body>
</html>
Gary