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

Form validation doesn't work with new script - why?

Status
Not open for further replies.

Julianne

Technical User
Jan 30, 2002
156
GB
I'm a newbie to ASP, so I've used a pre-made script to create a contact page that not only sends an e-mail via CDONTS but also writes the entries to a database.

In the past I've used the Check Form Dreamweaver extension (by Jaro von Flocken) to validate the text boxes, check box etc but this doesn't seem to work with this particular script (it has done with other ASP pages).

I don't get any error messages, but the form is still submitted even when fields are empty, which is what I don't want. I wonder if the Javascript is getting "taken over" by this line:

<%
if request.form(&quot;Submit&quot;)<> &quot;Submit&quot; then
%>

What does this line actually do? Can I take it out or replace it with something else?! The next line is just <form action=&quot;contact.asp&quot; method=&quot;POST&quot;>. Please let me know if you need me to post the page's code (it's quite long).

Many thanks.

Visit my ice hockey site at:
 
Without seeing code its hard to say anything, it looks like its submitting to itself and if it hasn't been posted to itself then display stuff to get details etc else send email. Post some of your code. Are you getting a javascript error on the page when its loaded?




 
OK, here's the whole code. The Javascript is what's generated by the Check Form Dreamweaver extension (which has worked well for me on pages with other ASP scripts). It adds an onSubmit to the <form> tag, but it's preceded by the ASP if request.form(&quot;Submit&quot;)... line.

<html>
<head>
<title>Add your site to our database</title>
<link href=&quot;test.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function YY_checkform() { //v4.66
//copyright (c)1998,2002 Yaromat.com
var args = YY_checkform.arguments; var myDot=true; var myV=''; var myErr='';var addErr=false;var myReq;
for (var i=1; i<args.length;i=i+4){
if (args[i+1].charAt(0)=='#'){myReq=true; args[i+1]=args[i+1].substring(1);}else{myReq=false}
var myObj = MM_findObj(args.replace(/\[\d+\]/ig,&quot;&quot;));
myV=myObj.value;
if (myObj.type=='text'||myObj.type=='password'||myObj.type=='hidden'){
if (myReq&&myObj.value.length==0){addErr=true}
if ((myV.length>0)&&(args[i+2]==1)){ //fromto
var myMa=args[i+1].split('_');if(isNaN(myV)||myV<myMa[0]/1||myV > myMa[1]/1){addErr=true}
} else if ((myV.length>0)&&(args[i+2]==2)){
var rx=new RegExp(&quot;^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$&quot;);if(!rx.test(myV))addErr=true;
} else if ((myV.length>0)&&(args[i+2]==3)){ // date
var myMa=args[i+1].split(&quot;#&quot;); var myAt=myV.match(myMa[0]);
if(myAt){
var myD=(myAt[myMa[1]])?myAt[myMa[1]]:1; var myM=myAt[myMa[2]]-1; var myY=myAt[myMa[3]];
var myDate=new Date(myY,myM,myD);
if(myDate.getFullYear()!=myY||myDate.getDate()!=myD||myDate.getMonth()!=myM){addErr=true};
}else{addErr=true}
} else if ((myV.length>0)&&(args[i+2]==4)){ // time
var myMa=args[i+1].split(&quot;#&quot;); var myAt=myV.match(myMa[0]);if(!myAt){addErr=true}
} else if (myV.length>0&&args[i+2]==5){ // check this 2
var myObj1 = MM_findObj(args[i+1].replace(/\[\d+\]/ig,&quot;&quot;));
if(myObj1.length)myObj1=myObj1[args[i+1].replace(/(.*\[)|(\].*)/ig,&quot;&quot;)];
if(!myObj1.checked){addErr=true}
} else if (myV.length>0&&args[i+2]==6){ // the same
var myObj1 = MM_findObj(args[i+1]);
if(myV!=myObj1.value){addErr=true}
}
} else
if (!myObj.type&&myObj.length>0&&myObj[0].type=='radio'){
var myTest = args.match(/(.*)\[(\d+)\].*/i);
var myObj1=(myObj.length>1)?myObj[myTest[2]]:myObj;
if (args[i+2]==1&&myObj1&&myObj1.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
if (args[i+2]==2){
var myDot=false;
for(var j=0;j<myObj.length;j++){myDot=myDot||myObj[j].checked}
if(!myDot){myErr+='* ' +args[i+3]+'\n'}
}
} else if (myObj.type=='checkbox'){
if(args[i+2]==1&&myObj.checked==false){addErr=true}
if(args[i+2]==2&&myObj.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
} else if (myObj.type=='select-one'||myObj.type=='select-multiple'){
if(args[i+2]==1&&myObj.selectedIndex/1==0){addErr=true}
}else if (myObj.type=='textarea'){
if(myV.length<args[i+1]){addErr=true}
}
if (addErr){myErr+='* '+args[i+3]+'\n'; addErr=false}
}
if (myErr!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+myErr)}
document.MM_returnValue = (myErr=='');
}
//-->
</script>
</head>
<body>
<!--#include file=&quot;head.asp&quot;-->
<!--#include file=&quot;menu.asp&quot;-->
<%
if request.form(&quot;Submit&quot;)<> &quot;Submit&quot; then
%>
<form action=&quot;submitsite.asp&quot; method=&quot;POST&quot; name=&quot;sitesubmissions&quot; onSubmit=&quot;YY_checkform('sitesubmissions','disclaimer','#q','1','Please tick the disclaimer box','EMail','S','2','Please enter your e-mail address','SiteTitle','#q','0','Please enter site title','URL','#q','0','Please enter URL');return document.MM_returnValue&quot;&quot;>
<table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr>
<td valign=&quot;top&quot;>Your Name<br>
<input name=&quot;Name&quot; type=&quot;text&quot; id=&quot;Name&quot; tabindex=&quot;1&quot;>
</td>
</tr>
<tr>
<td valign=&quot;top&quot;>E-Mail Address<br>
<input name=&quot;EMail&quot; type=&quot;text&quot; id=&quot;EMail&quot; tabindex=&quot;2&quot;>
</td>
</tr>
<tr>
<td valign=&quot;top&quot;>The Name/Title of Your Website<br>
<input name=&quot;SiteTitle&quot; type=&quot;text&quot; id=&quot;SiteTitle&quot; tabindex=&quot;3&quot;>
</td>
</tr>
<tr>
<td valign=&quot;top&quot;>Full URL (inc <input name=&quot;URL&quot; type=&quot;text&quot; id=&quot;URL&quot; tabindex=&quot;4&quot;>
</td>
</tr>
<tr>
<td valign=&quot;top&quot;>What is your site about?<br>
<textarea name=&quot;Aims&quot; rows=&quot;5&quot; cols=&quot;42&quot; tabindex=&quot;5&quot;></textarea>
</td>
</tr>
<tr>
<td valign=&quot;top&quot;>What software did you use to develop your site?<br>
<textarea rows=&quot;5&quot; name=&quot;Software&quot; id=&quot;Software&quot; cols=&quot;42&quot; tabindex=&quot;6&quot;></textarea>
</td>
</tr>
<tr>
<td valign=&quot;top&quot;>Please provide any other information that you feel may
be relevant:<br>
<textarea rows=&quot;5&quot; name=&quot;Info&quot; id=&quot;Info&quot; cols=&quot;42&quot; tabindex=&quot;7&quot;></textarea>
</td>
</tr>
<tr>
<td><p>
<input name=&quot;disclaimer&quot; type=&quot;checkbox&quot; id=&quot;disclaimer&quot; value=&quot;checkbox&quot; />
I have read and understand the terms and conditions.</p>
</td>
</tr>
<tr>
<td><input name=&quot;Submit&quot; type=submit value=&quot;Submit&quot;>
&nbsp;<input name=&quot;reset&quot; type=reset value=&quot;Reset&quot;>
</td>
</tr>
</table>
</form>
<%
else

strName=Request.form(&quot;Name&quot;)
strEMail=Request.form(&quot;EMail&quot;)
strSiteTitle=Request.form(&quot;SiteTitle&quot;)
strURL=Request.form(&quot;URL&quot;)
strAims=Request.form(&quot;Aims&quot;)
strSoftware=Request.form(&quot;Software&quot;)
strInfo=Request.form(&quot;Info&quot;)
strDate=Date

Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
ConnectionString=&quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;sitesubmissions.mdb&quot;)
objConn.Open ConnectionString

sqlQuery = &quot;SELECT * FROM sitesubmissions&quot;
Set objRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
objRS.Open sqlQuery, objConn, 1, 2

objRS.AddNew
objRS(&quot;Name&quot;)=strName
objRS(&quot;EMail&quot;)=strEMail
objRS(&quot;SiteTitle&quot;)=strSiteTitle
objRS(&quot;URL&quot;)=strURL
objRS(&quot;Aims&quot;)=strAims
objRS(&quot;Software&quot;)=strSoftware
objRS(&quot;Info&quot;)=strInfo
objRS(&quot;Date&quot;)=strDate

objRS.Update
objRS.Close
Set objRS = Nothing

strBody=strBody & &quot;Thanks for submitting your site. Below is a copy of the details you submitted:&quot; & chr(10) & chr(13)
strBody=strBody & &quot;Name: &quot; & strName & chr(10) & chr(13)
strBody=strBody & &quot;EMail: &quot; & strEMail & chr(10) & chr(13)
strBody=strBody & &quot;Site Title: &quot; & strSiteTitle & chr(10) & chr(13)
strBody=strBody & &quot;URL: &quot; & strURL & chr(10) & chr(13)
strBody=strBody & &quot;Aims and Functions: &quot; & strAims & chr(10) & chr(13)
strBody=strBody & &quot;Software Used: &quot; & strSoftware & chr(10) & chr(13)
strBody=strBody & &quot;Further Information: &quot; & strInfo & chr(10) & chr(13)
strBody=strBody & &quot;Date: &quot; & strDate & chr(10) & chr(13)
strBody=strBody & &quot;--------------- &quot; & chr(10) & chr(13)

Dim objCDO

Set objCDO = Server.CreateObject(&quot;CDONTS.NewMail&quot;)

objCDO.From = &quot;test@testsite.com&quot;
objCDO.To = &quot;test@testsite.com&quot;
objCDO.Subject = &quot;Site Submitted&quot;
objCDO.Body = strBody
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send

Set objCDO = Server.CreateObject(&quot;CDONTS.NewMail&quot;)

objCDO.From = &quot;test@testsite.com&quot;
objCDO.To = stremail
objCDO.Subject = &quot;Site Submitted&quot;
objCDO.Body = strBody
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send

Response.Write strName
Response.Write &quot;, we have received details that you sent about:<BR><BR>&quot;
Response.Write strSiteTitle
Response.Write &quot;, which is located at &quot; & strURL
Response.Write &quot;.<BR><BR>&quot;
Response.Write &quot;Click any of the links to continue browsing.&quot;


end if
%>
<!--#include file=&quot;foot.asp&quot;-->
</body>
</html>


Visit my ice hockey site at:
 
You have an extra &quot; at the end of this line, whether that is the problem?? but its nothing to do with your line

if request.form(&quot;Submit&quot;)<> &quot;Submit&quot; then

that I can see



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top