I am trying to validate this form before submisison to the DB.
THe problem is, when the name field is valid, it doesnt care to check wehther the email is also valid.
but, when name is invalid, say, blank, it checks to validate the email field.
I can not seem to get it worked out properly. ANy Suggestions?
Code:
<%@ Language=VBScript %>
<%DIM ErrorMsg, xFirst_Name, xLast_Name, xsocial_security,xaddress,xcity,xstate,xzip_code,xtelephone,xemail,xname%>
<%
const numFields = 2
dim errorArray()
redim preserve errorArray(numFields)
if request.form("isSubmitted") = "yes" then
xname = request.form("xname")
xemail = request.form("xemail")
ErrorMsg = ""
dim re
set re = New RegExp
'First Name
re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$"
re.Global = True
re.IgnoreCase = True
errorArray(0) = re.Test(xname)
if errorArray(0) then
errorArray(0)=False
else
errorArray(0) = True
ErrorMsg = "First Name<br>"
end if
'Email
re.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
errorArray(2) = re.Test(xemail)
if errorArray(2) then
errorArray(2) = False
else
errorArray(2) = True
ErrorMsg = ErrorMsg & "Email Address<br>"
end if
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- #INCLUDE FILE = "DataStore.inc" -->
<!-- #INCLUDE FILE = "adovbs.inc" -->
<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">
<%if ErrorMsg <> "" then %>
<font color="red" size="3"> <%= ErrorMsg %> </font>
<%end if%>
<form name="sample1" method="post">
<table width="50%" cellspacing="1" cellpadding="0" border="0">
<tr>
<td>
<% if errorArray(0) = True then %><font color="red"><b><% end if %>Name: <% if errorArray(0) = True then %></b></font><% end if %>
</td>
<td>
<input name="xname" value="<%= xname %>" size="20">
</td>
</tr>
<tr>
<td>
<% if errorArray(2) = True then %><font color="red"><b><% end if %>Email Address: <% if errorArray(2) = True then %></b></font><% end if %>
</td>
<td>
<input name="xemail" value="<%= xemail %>" size="20">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"><input type="hidden" name="isSubmitted" value="yes">
</td>
</tr>
</table>
</form>
<%if request.form("isSubmitted") = "yes" then
%>
<%
DIM all,i
all=False
For i=0 to 1
all=all OR CBOOL(errorArray(i))
Next
If Not all Then
'create variable to hold email address
DIM EMail
EMail = Request.Form("xemail")
Dim Name
Name = Request.Form("xname")
'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") = EMail
objRec("Name") = Name
'update the database with the new record
objRec.Update
'close the database
objRec.Close
Set objRec = Nothing
'un-comment this line to point to your desired confirm page
Response.Redirect("[URL unfurl="true"]http://www.robarspages.ca/devroot/computersite/aspvalid2.asp")[/URL]
xname = request.form("xname")
xemail = request.form("xemail")
end if
end if
%>
</body>
</html>
Gary