THAT Agency Design Studio Blog

With all the captcha’s out there right now why would anyone reinvent the wheel and write another one. Well I did and here is why.

When I build forms, my go-to validation is 9 out of 10 times its jQuery plugin: Validation. Keeping with that and their built in functionality to basically = something to something. I used that to my advantage and use PHP to create a simple captcha.



How it works

Let’s start with what you will need. 1. A Form, 2. The PHP, 3. The jQuery. Cool we got that? let’s move on.

Lets begin with the simple PHP that builds the math

1
2
3
4
5
<?php
$randomNum = rand(0,9);
$randomNum2 = rand(0,9);
$randomNumTotal = $randomNum + $randomNum2;
?>

Here we are basically setting two different numbers (randomNum and randomNum2) and getting their total in (randomNumTotal). You can read up on the rand() php function, but basically what it’s doing here is giving me a random 1 digit number from 0-9.



Now we need to set up our jQuery pieces. so here is what you would need to include inside of your <head> tag.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
	$.validator.methods.equal = function(value, element, param) {
		return value == param;
	};
	$(document).ready(function(){
		$("#yourform").validate({
				rules: {
					lastName: "required",
					math: {
						equal: <?php echo $randomNumTotal; ?>	
					}
				},
				messages: {
					lastName: "*",
					math: "*"
				}
			});
	});
	</script>



Now with that javascript done, all we need to is create the form and style it up. Here is a really simple form with a person’s first name and last name and the captcha.

THE CSS

1
2
3
4
5
6
<style>
table td{height:30px;}
form label.error, label.error{color:red;padding:0 0 0 10px;}
input.error,select.error,textarea.error{border:1px solid red;}
.required {color:red; font-weight:bold;}
</style>



THE FORM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<form action="" enctype="multipart/form-data" method="post" id="yourform">
<table cellpadding="0" cellspacing="0" border="0" width="500">
	<tr>
    	<td width="150" valign="top">First Name</td>
    	<td valign="top"><input name="firstName" id="firstName" type="text" size="25" maxlength="255"></td>
    </tr>
    <tr>
	    <td width="150" valign="top">Last Name</td>
		<td valign="top"><input name="lastName" id="lastName" type="text" size="25" maxlength="255"> <span class="required">*</span></td>
    </tr>
    <tr>
        <td width="150" valign="top" height="30">Security <input type="text" name="captchaImage" size="6" value="<?php echo $randomNum ?> + <?php echo $randomNum2 ?>" disabled="disabled" /></td>
        <td valign="top" height="30"><input type="text" name="math" id="math" size="6" maxlength="6" /> Please enter the correct result.</td>
    </tr>
    <tr>
        <td width="150">&nbsp;</td>
        <td valign="top"><input name="submit" type="submit" value="submit information" /></td>
    </tr>
</table>
</form>



And thats pretty much it. I would pressume that this can be changed for a bunch of other uses. maybe creating an array of the alphabet. selecting 1 letter, then the letter that follows and having the answer be the one after that (ex: A,B — answer: C). Or selecting two letters that are one letter apart and having the user select which one is in the middle (ex: A,C — answer: B).




-> Check out the live version of jQuery validate plugin plus PHP equals captcha




Well Here is the whole code for you. Remember it needs to be a PHP file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
# Set both random numbers you want to add
$randomNum = rand(0,9);
$randomNum2 = rand(0,9);
# Get the total.
$randomNumTotal = $randomNum + $randomNum2;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Using jQuery and PHP to create random addition problem as a captcha</title>
<style>
table td{height:30px;}
form label.error, label.error{color:red;padding:0 0 0 10px;}
input.error,select.error,textarea.error{border:1px solid red;}
.required {color:red; font-weight:bold;}
</style>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
	$.validator.methods.equal = function(value, element, param) {
		return value == param;
	};
	$(document).ready(function(){
		$("#yourform").validate({
				rules: {
					lastName: "required",
					math: {
						equal: <?php echo $randomNumTotal; ?>	
					}
				},
				messages: {
					lastName: "*",
					math: "*"
				}
			});
	});
	</script>
</head>
<body>
<p>Example of using jQuery and PHP to create random addition problem as a captcha</p>
<br/>
<form action="" enctype="multipart/form-data" method="post" id="yourform">
<table cellpadding="0" cellspacing="0" border="0" width="500">
	<tr>
    	<td width="150" valign="top">First Name</td>
    	<td valign="top"><input name="firstName" id="firstName" type="text" size="25" maxlength="255"></td>
    </tr>
    <tr>
	    <td width="150" valign="top">Last Name</td>
		<td valign="top"><input name="lastName" id="lastName" type="text" size="25" maxlength="255"> <span class="required">*</span></td>
    </tr>
    <tr>
        <td width="150" valign="top" height="30">Security <input type="text" name="captchaImage" size="6" value="<?= $randomNum ?> + <?= $randomNum2 ?>" disabled="disabled" /></td>
        <td valign="top" height="30"><input type="text" name="math" id="math" size="6" maxlength="6" /> Please enter the correct result.</td>
    </tr>
    <tr>
        <td width="150">&nbsp;</td>
        <td valign="top"><input name="submit" type="submit" value="submit information" /></td>
    </tr>
</table>
</form>
</body>
</html>

Share this article

If you know me, you know that I am a huge fan of jQuery. I love the ease of use and the simple learning curve. One of the great things I love about jQuery is the ability to implement plugins. And with a huge community behind it, there are many plugins that you can use to create more interactive websites.

jGrow

jGrow

jGrow is a jQuery plug-in that makes the textarea adjust its size according to the length of the text.
It works smoothly with jQuery 1.2.x and 1.3.x. It was tested on IE6+, Firefox 2+, Opera 9.x+ (including 10.00 beta), Safari 3.x+ (including 4 Public Beta), Google Chrome 2.x (including developer release) and no bug was spotted yet.

__________________________________________________________________

jQuery Link Nudge Plugin

link-nudge

It started with a MooTools version and shortly thereafter a jQuery version. Just recently Drew Douglass premiered a jQuery plugin that aimed at producing the same type of effect. I’ve taken some time to put together my own version of the jQuery nudging plugin.

__________________________________________________________________

jQuery Tools

jQuery-Tools

jQuery Tools is a collection of the most important user-interface components for today’s websites. This single JavaScript file weighs only 5.59 Kb

__________________________________________________________________

FriendFeed JSONP Widget

FriendFeed-JSONP-Widget

A simple JSONP based widget to get a user timeline. Support for optional overrides.

__________________________________________________________________

Internal Links with Favicon using jQuery

internal-links

jQuery Plugin that would allow all the internal links in your website to actually have your own favicon implemented on the left side of the link.

__________________________________________________________________

Uploadify

uploadify

Uploadify is a jQuery plugin that allows the easy integration of a multiple (or single) file uploads on your website.  It requires Flash and any backend development language.  An array of options allow for full customization for advanced users, but basic implementation is so easy that even coding novices can do it.

__________________________________________________________________

myPrintArea

myPrintArea

PrintArea sends a specified dom element to the printer, maintaining the original css styles applied to the element being printed. 2 different modes of printing, iframe and popup, now give greater flexibility! Along with several popup options.

__________________________________________________________________

Pan View

panView

Creates a pan view, hold down your mouse button and start moving.

__________________________________________________________________

Simple Modal

simple-model

SimpleModal is a lightweight jQuery plugin that provides a simple interface for creating a modal dialog. The goal of SimpleModal is to provide developers with a cross-browser overlay and container that will be populated with data provided to SimpleModal.

Know any more? let us know.

Share this article

This is a JQuery plugin I have been working on to try to eliminate; if not help, fight spam through bots and such going through websites and scraping email addresses.

Basically it works like this:  You add the emails in a certain way in your code, be it yourname^yourcompany#com or however you want to add it. Then let the plugin work its magic. The code will be shown as above, but will behave as if you had typed in yourname@yourcompany.com. Simple huh? Well here we go:

Javascript

1
2
3
4
5
6
7
8
9
10
<script>
$(document).ready(function(){
// example 1
$('a[rel*=spemail]').spemail('|,:','linkbase');
// example 2
$('#email').spemail('|,:','mailbase');
// example 3
$('#plainemail').spemail('|,:','plainbase');
});
</script>

HTML

1
2
3
<p><strong>Example 1:</strong> <a href="yourname|yourcompany:com" rel="spemail"></a></p>
<p><strong>Example 2:</strong> <span id="email">yourname|yourcompany:net</span></p>
<p><strong>Example 3:</strong> <span id="plainemail">yourname|yourcompany:net</span></p>

As you can see in the examples above, the code in the DOM, will handle specific email pieces based on their ID or rel. Try it out and see. It works well.

Check out a live example of Fighting spam from inline email addresses using JQuery

or

Download the Fighting spam from inline email addresses using JQuery Plugin – Version 1.0 source files

Share this article