Link to home
Start Free TrialLog in
Avatar of TrueBlue
TrueBlueFlag for United States of America

asked on

Trying to incorporate following code into existing javascript

Hello,

I am trying to incorporate the following code into an existing javascript that checks data inputted into a form.
I added a field just above the submit button on the asp form that incorporates captcha to reduce the amount of spam we are getting. The field and value is working fine. Just need a way to keep someone from getting past entering the correct value before the email is sent.

I need to add at the top somehow:

<!-- Include file for CAPTCHA form processing -->            
<!-- #include file="CAPTCHA/CAPTCHA_process_form.asp" -->

And some kind of code to check the variable (blnCAPTCHAcodeCorrect) for a false condition and then clear the field and give the below error: (I know this is vbscript, but I do not know the javascript equivalent syntax).

 If blnCAPTCHAcodeCorrect = False Then Response.Write(" CAPTCHA code is NOT correct, please press the Load New   Code link and enter the new value")
End If

Here is the code that creates the field, etc in the asp form:

<table width="100%" border="0" cellspacing="1" cellpadding="3">
 <tr>
  <td><img src="CAPTCHA/CAPTCHA_image.asp" alt="Code Image - Please contact webmaster if you have problems seeing this image code" id="CAPTCHA" />&nbsp;<a href="javascript:reloadCAPTCHA();"><% = strTxtLoadNewCode %></a></td>
 </tr>
 <tr>
  <td><input type="text" name="securityCode" id="securityCode" size="12" maxlength="12" autocomplete="off" /></td>
 </tr><%

Here is the working script for the form:

// <!--
function validateForm(theForm)
{
     var isPhone = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/; // (000)000-0000 or (000) 000-0000
     var isEmail = /^[a-z][a-z0-9]*([.\-_][a-z][a-z0-9]*)*@([a-z][a-z0-9]*.)*([a-z]{2}|com|net|org|biz|gov|pro|int|mil|edu|info|name|aero|coop|museum)$/i;
     if(theForm.Name.value.split(" ").join("") == "")
     {
          alert("Please enter your Name.");
          theForm.Name.select();
          theForm.Name.focus();
          return (false);
     }
     if(theForm.Name.value.length > 70)
     {
          alert("Please enter at most 70 characters in the Name field.");
         theForm.Name.focus();
         return (false);
     }
     if(theForm.EmailAddress.value.split(" ").join("") == "")
     {
          alert("Please enter a Email Address.");
          theForm.EmailAddress.select();
          theForm.EmailAddress.focus();
          return (false);
     }
     if (!isEmail.test(theForm.EmailAddress.value))
     {
         alert("The Email Address is NOT in the corect format.");
         theForm.EmailAddress.focus();
         return (false);
     }
      if (theForm.EmailAddress.value.length > 35)
       {
         alert("Please enter at most 35 characters in the Email Address field.");
         theForm.EmailAddress.focus();
         return (false);
       }
     if (theForm.DaytimePhone.value.split(" ").join("") == "")
     {
          alert("Please enter a Daytime Phone Number.");
          theForm.DaytimePhone.select();
          theForm.DaytimePhone.focus();
          return (false);
     }
     if (!isPhone(theForm.DaytimePhone.value))
     {
          alert("The Phone Number is NOT in the correct format:\n...(000)000-0000 or (000) 000-0000.");
          theForm.DaytimePhone.focus();
          return (false);
     }
     if (theForm.DaytimePhone.value.length > 14)
     {
          alert("Please enter at most 14 characters in the \"Daytime Phone\" field.");
          theForm.DaytimePhone.focus();
          return (false);
     }
     return (true);
}
// -->

Hope this makes sense.

Thank you in advance.
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

TrueBlue,

That vbscript code looks more like server side script than client side vbscript.  I can't say for sure without seeing the rest of the instructions for using the script but usually this type of verification is done on the server side.  Even the error message (Response.Write) is server side code.  I wouldn't worry about converting it to client side javascript because it won't work.  Please provide more information on this Captcha component if you have questions about using it on your site.

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of TrueBlue

ASKER

Here are the instructions:

1. Place the folder, and it's contents, named 'CAPTCHA' into the same directory that your
web form you wish to integrate Web Wiz CAPTCHA into is within.


2. The web page that contains the HTML web form you wish to integrate Web Wiz CAPTCHA into must
have an .asp extension (eg. my_own_form_file.asp (this is an example file name and not a real
file))


3. Open your web form in a text editor and place the following code into the part of your
form where you wish the CAPTCHA image and textarea to be:-

      <!-- include the Web Wiz CAPTCHA form -->
      <!--#include file="CAPTCHA/CAPTCHA_form_inc.asp" -->

      
4. Open the file in a text editor that is to process your web form input, and place the
following code at the top of the file (not within ASP blocks):-

      <!-- Include file for CAPTCHA form processing -->            
      <!-- #include file="CAPTCHA/CAPTCHA_process_form.asp" -->

      
5. Now within the file you entered the code from the last step into you can call the following
variable to check that the CAPTCHA code entered is correct.

      blnCAPTCHAcodeCorrect
      
If the CAPTCHA code entered is correct the above variable with be set to true, if the CAPTCHA code
has not been entered correctly the variable will be set to false.


      5.1 Below is some sample code to check the CAPTCHA code is correct:-
      
            <%

            If blnCAPTCHAcodeCorrect = True Then
                  Response.Write(" CAPTCHA code is correct")
            ElseIf  blnCAPTCHAcodeCorrect = False Then
                  Response.Write(" CAPTCHA code is NOT correct")
            End If

            %>

Hope this helps.
Those steps are good.  Thanks for the information.  Do you have a question about implementing specific step?  I was correct that this is all server side and you don't need to convert any part to javascript.

You will need 2 asp pages.  The first will display the form and the CAPTCHA image.  Steps 2 & 3 are used on this page.  Do you see the image?

The last 2 steps are put in the page that you send the form to.  Usually this will do server side validation.  As part of that validation you can test to see if blnCAPTCHAcodeCorrect is true.  If so then continue with the server side validation.  If not, then you should send the visitor back to the form and ask them to enter the correct text.  This is not the javascript validation done with the validateForm() function.

Let me know if you have a question about this or any of the steps.  Please confirm that the form is an asp page and it is submitted to a second asp page.  You can provide the file names to make it easier for me to refer to them.  If you have a question about how to test for it to be true/false then let me see your current asp validation page and let me know what you want to do if it is false.  Remember that this can't be included or done with the javascript function you have on the form page that does separate validation before the form is submitted.  I hope this helps.

bol
Bolscott,

Yes, I have the image appearing on my form above my submit button.

Here is the page that is called by pressing the submit button (the one that actually generates the email and the one that is supposed to check the value of thevariable blnCAPTCHAcodeCorrect is true or false.
I do not want an email sent if the variable is false. I would like the person just returned to the form with an error saying you incorrectly entered the captcha, hopefully the form would still have the other values they already entered.
So I need help incorporating this into this asp code.

<% If "" & Request("EmailAddress") <> "" Then
    Set Mail = Server.CreateObject("Persits.MailSender")
    Mail.Host = "mail.example.net" 'specify valid SMTP host
    Mail.From = Request("EmailAddress") 'specify senders address
    Mail.FromName = Request("Name") 'specify senders name
    Mail.AddAddress "sales@example.net"                      
    'Mail.AddCC "admin@example.net"
    'Mail.AddCustomHeader "Return-Receipt-To: <sales@example.net>"
    'Mail.AddCustomHeader "Disposition-Notification-To: <sales@example.net>"  
    Mail.Subject = Request("Subject")
    'Build message body
    Body = "IP Address: " & Request.ServerVariables("REMOTE_HOST") & chr(13) & chr(10)
    Body = Body & "Daytime Phone No: " & Request("DaytimePhone") & chr(13) & chr(10)
    Body = Body & "Address: " & Request("StreetAddress") & chr(13) & chr(10)
    Body = Body & "City: " & Request("City") & chr(13) & chr(10)
    Body = Body & "State: " & Request("State") & chr(13) & chr(10)
    Body = Body & "Zip Code: " & Request("ZipCode") & chr(13) & chr(10)
    Body = Body & "Message: " & Request("Message") & chr(13) & chr(10)
 
    Mail.Body = Body ' assign string to Mail.Body
 
    On Error Resume Next
    Mail.Send()
    Set Mail = Nothing
    Response.Redirect("thankyou.htm")
    If Err <> 0 Then
      Response.Write "Error encountered: " & Err.Description
    End If
     Else %>
<script language="JavaScript" type="text/javascript">javascript: window.history.back(-1)</script>
<% End IF %>

Thank you in advance.
Thanks for the code.  I have modified it to do the check and it will go back to the form page.  You need to change the Response.redirect line so that it goes to the correct page (the form page).  Leave the querystring in the url so that the form page can search for it and print an error.  I can show you how to do that if you need.  Also you can fill in the form fields that were correct by using session variables or passing them back to the form page in the querystring.  Let me know if you need details about doing any of these steps.  I will need the script for the form page if you need that help.

The modified code below uses an if statement to redirect if the value isn't True.  If it is then it continues with your script and sends the email.  I would recommend that you change the script line that is done with the else to a redirect.  That javascript line isn't the best way to do it since everything else is server script.  Use the server script redirect command.

Let me know if you have any questions.

<% If "" & Request("EmailAddress") <> "" Then
    If blnCAPTCHAcodeCorrect <> True then
       Response.Redirect "formpage.asp?err=captcha"
    Else
    Set Mail = Server.CreateObject("Persits.MailSender")
    Mail.Host = "mail.example.net" 'specify valid SMTP host
    Mail.From = Request("EmailAddress") 'specify senders address
    Mail.FromName = Request("Name") 'specify senders name
    Mail.AddAddress "sales@example.net"                      
    'Mail.AddCC "admin@example.net"
    'Mail.AddCustomHeader "Return-Receipt-To: <sales@example.net>"
    'Mail.AddCustomHeader "Disposition-Notification-To: <sales@example.net>"  
    Mail.Subject = Request("Subject")
    'Build message body
    Body = "IP Address: " & Request.ServerVariables("REMOTE_HOST") & chr(13) & chr(10)
    Body = Body & "Daytime Phone No: " & Request("DaytimePhone") & chr(13) & chr(10)
    Body = Body & "Address: " & Request("StreetAddress") & chr(13) & chr(10)
    Body = Body & "City: " & Request("City") & chr(13) & chr(10)
    Body = Body & "State: " & Request("State") & chr(13) & chr(10)
    Body = Body & "Zip Code: " & Request("ZipCode") & chr(13) & chr(10)
    Body = Body & "Message: " & Request("Message") & chr(13) & chr(10)
 
    Mail.Body = Body ' assign string to Mail.Body
 
    On Error Resume Next
    Mail.Send()
    Set Mail = Nothing
    Response.Redirect("thankyou.htm")
    If Err <> 0 Then
      Response.Write "Error encountered: " & Err.Description
    End If
    End if
     Else %>
<script language="JavaScript" type="text/javascript">javascript: window.history.back(-1)</script>
<% End IF %>
Hi Scott,

I greatly appreciate your patience.

If you would not mind showing me what you mean with the session variables, etc.

The form page is named contact-us.asp and here the code.

<FORM ACTION="sendeform.asp" METHOD="POST" onsubmit="return validateForm(this)" name="Form1">
  <p><font face="Arial" size="2">Name </font>  
  <font face="Arial" size="2" color="#000080">(required)</font><br>
  <input type="text" name="Name" size="40" maxlength="70">&nbsp;<br>
  <font face="Arial" size="2">  E-mail Address </font>
  <font face="Arial" size="2" color="#000080">(required)</font><font face="Arial" size="2"> </font>
   <font face="Arial" size="2">  <br>
    <input type="text" name="EmailAddress" size="35" maxlength="35"><BR>
  </font>
   <font face="Arial" size="2">Subject</font><font face="Arial" size="2"><BR>
   </font>
   <select name="Subject" size="1">
   <option selected value="Other">Please Select</option>
   <option value="Acceptable Use Violation">Acceptable Use Violation</option>
   <option value="Billing Question">Billing Question</option>
   <option value="Custom Quote">Custom Quote</option>
   <option value="Service Request">Service Request</option>
   <option value="Technical Support">Technical Support</option>
   <option value="Web Site Access">Web Site Access</option>
   </select><BR>

   <font face="Arial" size="2">Daytime Phone </font>
   <font face="Arial" size="2" color="#000080">(required)</font><font face="Arial" size="2"><BR>
   <INPUT maxLength="12" name="DaytimePhone" size="12" onfocus="doSubmit=false;"
    onblur="checkPhone(this);" onkeyup="phoneMask(this);" onkeydown="phoneMask(this);"
    value=""/><br>
  </font>
 
  <font face="Arial" size="2">Street Address</font><font face="Arial" size="2"><BR>  
   <INPUT TYPE="text" NAME="StreetAddress" SIZE="45" /><BR>  
  </font>
 
  <font face="Arial" size="2">City</font><font face="Arial" size="2"><BR><INPUT TYPE="text" NAME="City" SIZE="45" /><BR>  
  </font>
 
  <font face="Arial" size="2">State</font><font face="Arial" size="2"><BR>  
  </font>
 
   <select name="State" size="1">
   <option selected value="FL">Please Select</option>
   <option value="FL">FLORIDA</option>
   <option value="GA">GEORGIA</option>
   <option value="TN">TENNESSEE</option>
   <option value="SC">SOUTH CAROLINA</option>
   <option value="NC">NORTH CAROLINA</option>
   </select><font face="Arial" size="2"><BR>  
  </font>
 
  <font face="Arial" size="2">Zip Code</font><font face="Arial" size="2"><BR><INPUT TYPE="text" NAME="ZipCode" SIZE="15"><BR>  
  </font>
 
  <font face="Arial" size="2">Enter Question Here</font>
  <font face="Arial" size="2"><BR>
  <TEXTAREA COLS=50 ROWS=12 NAME="message"></TEXTAREA>
 
<!-- include the Web Wiz CAPTCHA form -->
<!--#include file="CAPTCHA/CAPTCHA_form_inc.asp" -->

  <INPUT TYPE="submit" VALUE="Only press once to send" />
  <INPUT TYPE="reset" VALUE="Clear" />  
  </font>
  </FORM>

Thank you in advance.
No, problem.  I'm glad to help.  However if you need a lot of details on this then you may want to open a new question for it.

First, to help with sending back to the form and reporting the error.  The response.redirect I added to sendeform.asp could look like this ...

       Response.Redirect "contact-us.asp?err=captcha"

On the contact-us.asp page you could put some code like this to check for and display the error.

<%
If Request.Querystring("err") = "captcha" then
%>
<div align="center">There was an error with the letters you entered.  Please look at the image in the form and type the letters you see.</div>
<%
End if
%>

You can modify the code above to change the message and the way it displays.  That should at least give you an idea of how to do it.

To fill the form fields you would use code like this on the contact-us.asp ...

  <input type="text" name="Name" size="40" maxlength="70" value="<%= Request("name") %>">&nbsp;<br>

I just used one field to illustrate how you set the value with ASP.  Notice the script will look to see if a variable named "name" has been set and then print its value in the value attribute of the form's input field.

Now you can send the value back to the form from sendeform.asp using either the querystring or a session variable.  An example of setting a session variable is below.

' after validating and verifying the value is good
Session("name") = Request("name") ' or the name of the variable

You can change "name" to match whatever name you want to give the session variable.

Let me know if you have any questions about this or need more details.

bol
Bol,

I am confused. It has been quite a while since I worked with this script.
I created another question if you could provide the code you suggest into my code.
https://www.experts-exchange.com/questions/22073513/trying-to-incorporate-captcha-into-an-existing-asp-form-and-email-routine.html

It would be greatly appreciated.

Chuck
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm glad that I could help you with this.  Thank you for the grade, the points and the fun question.

bol