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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation Slightly Off 1

Status
Not open for further replies.

gwillr

IS-IT--Management
Nov 4, 2003
267
CA

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

 
gwillr,

Email is on errorArray(2) not on errorArray(1). Whereas, you check far down there:
[tt]
For i=0 to [red]1[/red]
all=all OR CBOOL(errorArray(i))
Next
[/tt]
(Don't know the impact of your real errorArray(1) which is not shown anywhere.)

regards - tsuji
 
Thanks Tsuji,

That did the trick! Its funny how things like that slip by after looking at the screen for a few hours!

Thanks again!
Cheers

Gary

 
gwillr,

Thanks. To make it more coherent, you can either use numFields or ubound(errorArray) as the upperbound so that the script becomes more tightly knitted together as a whole.

- tsuji
 
Thanks Tsuji!

I have done some research on email validation, and and had run across this code for actually checking the domain for validity:

Code:
<%@LANGUAGE="VBSCRIPT"%>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>MX Lookup</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="MXlookup.asp" method="get">
<p> Enter the host name for MX lookup:&nbsp;&nbsp;&nbsp;
<input name="host" type="text" id="host" size="40" value="<%=Request.QueryString("host")%>" />
&nbsp; &nbsp; &nbsp;
<input type="submit" value=" Lookup " />
</p>
</form>
<%
IF Request.QueryString("host")<>"" THEN
  result = MXLookup(Request.QueryString("host"))
  IF result = TRUE THEN
    Response.Write "Accepted."
  ELSE
    Response.Write "Denied!"
  END IF
END IF
FUNCTION MXLookup(host)
  MXLookUp = False
  Dim objXMLHTTP,strResult
  Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
  objXMLHTTP.Open "Get", _
    "[URL unfurl="true"]http://examples.softwaremodules.com/IntraDns.asp?domainname="[/URL] & host & "&Submit=Submit&t_mx=1", False
  objXMLHTTP.Send
  strResult = objXMLHTTP.ResponseText
  strResult = Mid(strResult,InStr(strResult,"(MX) for <strong>"),100)
  strResult = Mid(strResult,Instr(strResult,"</strong>. Items Returned: <strong>")+35,1)
  IF CInt(strResult) > 0 THEN
    MXLookUp = TRUE
  ELSE
    MXLookUp = FALSE
  END IF
END FUNCTION
%>

aside from it being slow at times, is there an easy way that you see to implement this in to what I already have?

If no, I am also stuck on how to implement a slightly less strict alternative; something like :

Code:
/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/

I would want to check the TLD to ensure it was = to one of these, but im not sure of the correct syntax, in relation to what I already have in place.

Comments & suggestion appreciated.

Gary

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top