I am trying to get this classic captcha to validate when the wrong code is entered into the form and submitted but it is not working.
Here is the cpatcha code as implemented in my webform and the asp script that runs the form. All help appreciated.
The form file
My ASP file
It does send the email but it won't validate even if someone enters the the wrong captcha code or no code.
Here is the cpatcha code as implemented in my webform and the asp script that runs the form. All help appreciated.
The form file
Code:
<script language="JavaScript" type="text/JavaScript"> <!-- function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>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[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_validateForm() { //v4.0 var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error(s) occurred:\n'+errors); document.MM_returnValue = (errors == ''); } //--> </script> <script language="javascript"> function RefreshImage(valImageId) { var objImage = document.images[valImageId]; if (objImage == undefined) { return; } var now = new Date(); objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString(); } </script> <form method="POST" action=""> <p>Fields marked (*) are required </p> <table width="642" border="0"> <tr> <td width="155">Full Name: (*) </td> <td width="296"><input type="text" name="Name"></td> <td width="177" rowspan="15"> </td> </tr> <tr> <td>Date of Birth: (*) </td> <td><input type="text" name="DateofBirth"></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="Address"></td> </tr> <tr> <td>Address 2: </td> <td><input type="text" name="Address2"></td> </tr> <tr> <td>Parish:</td> <td><input type="text" name="Parish"></td> </tr> <tr> <td>Post Code: </td> <td><input type="text" name="PostCode"></td> </tr> <tr> <td>Telephone:</td> <td><input type="text" name="Telephone"></td> </tr> <tr> <td>Email: (*) </td> <td><input type="text" name="EmailFrom"></td> </tr> <tr> <td rowspan="2">testing: (*) </td> <td><p> <input name="testing" type="checkbox" value=""> <input type="checkbox" name="testing" value=""> <input type="checkbox" name="testing" value=""> l </p> </td> </tr> <tr> <td><input type="checkbox" name="testing" value=" "> <input type="checkbox" name="testing" value="Other"> Other </td> </tr> <tr> <td>If Other, please specify: (*) </td> <td><input type="text" name="IfOtherPleaseSpecify"></td> </tr> <tr> <td>Current Number: (*)</td> <td><input type="text" name="CurrentNumber"></td> </tr> <tr> <td>Additional Comment: </td> <td><textarea name="Comment"></textarea></td> </tr> <tr> <td>Test2:</td> <td><input type="url" name="test2" style="width: 250px" value="http://www.mysite/default.asp"></td> </tr> <tr> <td width="261">CAPTCHA Image</td> <td width="123"><img id="imgCaptcha" src="captcha.asp" /><br /><a href="javascript:void(0)" onclick="RefreshImage('imgCaptcha')">Change Image</a></td> </tr> <tr> <td>Write the characters in the image above</td> <td><input name="captchacode" type="text" id="captchacode" size="10" /></td> </tr> <tr> <tr> <td><input type="reset" name="Reset" value="Reset"> <input type="submit" name="btnTest" id="btnTest" onClick="MM_validateForm('Name','','R','DateofBirth','','R','EmailFrom','','R','CurrentNumber','','R');return document.MM_returnValue" value="Submit"></td> <td> </td> </tr> </table> <p> </p> <p></form>
Code:
<% function TestCaptcha(byval valSession, byval valCaptcha) dim tmpSession valSession = Trim(valSession) valCaptcha = Trim(valCaptcha) if (valSession = vbNullString) or (valCaptcha = vbNullString) then TestCaptcha = false else tmpSession = valSession valSession = Trim(Session(valSession)) Session(tmpSession) = vbNullString if valSession = vbNullString then TestCaptcha = false else valCaptcha = Replace(valCaptcha,"i","I") if StrComp(valSession,valCaptcha,1) = 0 then TestCaptcha = true else TestCaptcha = false end if end if end if end function %> <% ' declare variables Dim EmailFrom Dim EmailTo Dim Subject Dim Name Dim Address Dim Telephone Dim test2 Dim DateofBirth Dim Address2 Dim Parish Dim PostCode Dim Testing Dim CurrentNumber Dim IfOtherPleaseSpecify Dim Comment ' get posted data into variables EmailFrom = Trim(Request.Form("EmailFrom")) EmailTo = "[email protected]" Subject = "Please " Name = Trim(Request.Form("Name")) Address = Trim(Request.Form("Address")) Telephone = Trim(Request.Form("Telephone")) test2 = Trim(Request.Form("test2")) DateofBirth = Trim(Request.Form("DateofBirth")) Address2 = Trim(Request.Form("Address2")) Parish = Trim(Request.Form("Parish")) PostCode = Trim(Request.Form("PostCode")) testing = Trim(Request.Form("testing")) CurrentNumber = Trim(Request.Form("CurrentNumber")) IfOtherPleaseSpecify = Trim(Request.Form("IfOtherPleaseSpecify")) Comment = Trim(Request.Form("Comment")) ' validation Dim validationOK validationOK=true If (Trim(EmailFrom)="") Then validationOK=false If (Trim(Name)="") Then validationOK=false If (validationOK=false) Then Response.Redirect("error.htm?" & EmailFrom) ' prepare email body text Dim Body Body = Body & "Full Name: " & Name & VbCrLf Body = Body & "Date of Birth: " & DateofBirth & VbCrLf Body = Body & "Address: " & Address & VbCrLf Body = Body & "Address2: " & Address2 & VbCrLf Body = Body & "Parish: " & Parish & VbCrLf Body = Body & "Post Code: " & PostCode & VbCrLf Body = Body & "Telephone: " & Telephone & VbCrLf Body = Body & "testong: " & testing & VbCrLf Body = Body & "If Other please specify: " & IfOtherPleaseSpecify & VbCrLf Body = Body & "Current Number: " & CurrentNumber & VbCrLf Body = Body & "Comment: " & Comment & VbCrLf Body = Body & "test2: " & test2 & VbCrLf ' send email Dim mail Set mail = Server.CreateObject("CDONTS.NewMail") mail.To = EmailTo mail.From = EmailFrom mail.Subject = Subject mail.Body = Body mail.Send if not IsEmpty(Request.Form("btnTest")) then Response.Write("<tr><td colspan=""2"" align=""center"">") if TestCaptcha("ASPCAPTCHA", Request.Form("captchacode")) then Response.Write("<b style=""color:#00CC33"">The code you enter verified.</b>") else Response.Write("<b style=""color:#FF0000"">You entered the wrong code.</b>") end if Response.Write("</td></tr>" & vbCrLf) end if ' redirect to success page Response.Redirect("thanks.asp?" ) %>
It does send the email but it won't validate even if someone enters the the wrong captcha code or no code.
Comment