Sample Math CAPTCHA to Add to an Anonymous Contact / Feedback Form

Mindwatering Incorporated

Author: Tripp W Black

Created: 12/02/2015 at 12:33 PM

 

Category:
Notes Developer Tips
Formulas

Goal:
Need to add a math validation check, to an anonymous feedback/contact web form, so that spammers have harder time submitting thousands of bogus contact forms.


Solution:
Add 3 fields to your feedback form. They should look similar to below:


The FB_C3 field is hidden. In the browser, they will look similar to below:

Notes:
The C1 and C2 fields cannot be computed when composed, or they will actually recompute again during submission. Make them editable.
Add FB_A1 to your form's validation onsubmit function to ensure it has a value / number value.

FB_C1 - Editable Text
Default Value:
tmp:= @Round(7 * @Random - 1);
@Text(tmp)

Input Translation:
@ThisValue

HTML Attributes:
"size=\"2\" readonly = \"readonly\""

Style:
border: 0px;

FB_C2 - Editable Text
Default Value:
tmp:= @Round(5 * @Random + 1);
tmp2:= @If(tmp=0; "zero";
tmp=1; "one";
tmp=2; "two";
tmp=3; "three";
tmp=4; "four";
tmp=5; "five";
tmp=6; "six";
tmp=7; "seven";
tmp=8; "eight";
tmp=9; "nine";
tmp=10; "ten";
@Text(tmp));
tmp2

Input Translation:
@ThisValue

HTML Attributes:
"size=\"8\" readonly = \"readonly\""

Style:
border: 0px;


FB_A1 - Editable Text
Default Value:
""

Input Translation:
@Trim(@ThisValue)

HTML Attributes:
"size=\"2\" maxlength=\"3\""


FB_C3 - Computed Number
Value:
tmpstr:= FB_C2;
tmp2:= @If(tmpstr="zero"; 0;
tmpstr="one"; 1;
tmpstr="two"; 2;
tmpstr="three"; 3;
tmpstr="four"; 4;
tmpstr="five"; 5;
tmpstr="six"; 6;
tmpstr="seven"; 7;
tmpstr="eight"; 8;
tmpstr="nine"; 9;
tmpstr="ten"; 10;
tmpstr=""; 0;
@ToNumber(tmpstr));

tmp:= @ToNumber(FB_C1) + tmp2;
@If(@IsError(tmp); 0; tmp)


WQS Agent Code Snippets:
Dim s As New NotesSession
Dim doc As NotesDocument ' this contact doc
...
Dim fbc1 As String
Dim fbc2 As String
...
' check the math
fbc1 = Trim$(CStr(doc.FB_C3(0) ) ) ' answer on form
fbc2 = Trim$(CStr(doc.FB_A1(0) ) )
If Not (DoCInt(fbc1) = DoCInt(fbc2)) Then
Print "Your answer: " & fbc2 & ". Correct answer: " & fbc1 & "."
Print |Sorry. <br><br>The math question appears incorrect. <br><a href="javascript:window.history.back();">back</a><br><br>Thank you.|
Exit Sub
End If
...
Exit Sub

%REM
Function DoCInt
Description: wrapper function for CInt
%END REM
Function DoCInt(numstr As String) As Integer
On Error GoTo FErrorHandler

If (numstr="") Then
DoCInt = 0
Exit Function
End If
DoCInt = CInt(numstr)

FExit:
Exit Function
FErrorHandler:
DoCInt = 0
Resume FExit
End Function


previous page