/    Sign up×
Community /Pin to ProfileBookmark

[newbie] Anti-SPAM + form validator?

Hello

This must be a very simple problem for JS experts, but I don’t know enough yet about JS to know how to solve it.

After successfully adding and using jQuery’s form validator, I’d like to add a simple “Please computer a+b” anti-SPAM check.

The problem is how to combine the two?

Here’s a simplified example:

[code]
<head>
<script src=”http://code.jquery.com/jquery.min.js”></script>
<script type=”text/javascript” src=”js/jquery.validate.js”></script>

<script type=”text/javascript”>
//========= jQuery form validator
jQuery.extend(jQuery.validator.messages, {
required: “This field is required.”,
email: “Please enter a valid e-mail address.”
});

//========= Simple, anti-spam scheme
var a = Math.ceil(Math.random() * 10);
var b = Math.ceil(Math.random() * 10);
var c = a + b

function DrawBotBoot()
{
document.write(“Compute “+ a + ” + ” + b +”? “);
document.write(“<input id=’BotBootInput’ type=’text’ maxlength=’2′ size=’2’/>”);
}
function ValidBotBoot(){
var d = document.getElementById(‘BotBootInput’).value;
if (d == c) return true;
return false;
}

//========= ?How to check anti-spam answer + validate other fields in form?
$(document).ready(function(){
$(“#myform”).validate({
/*submitHandler: function(form) {
form.submit();
}
});*/
};
</script>
</head>

<body>

<form action=”send.php” method=”post” id=”myform”>
Name <input type=”text” maxlength=”25″ id=”name” name=”name” class=”required”/>
Anti-SPAM check<script type=”text/javascript”>DrawBotBoot()</script>

<input type=”submit” class=”submit” value=”Send” onclick=”alert(ValidBotBoot());”/>
</form>

</body>
[/code]

Thanks for any help.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@007JulienNov 29.2012 — 1/ - Do not use document.write in a function which run after the document is loaded !

2/ - [CODE]
[COLOR="#0000FF"]// this two lines[/COLOR]
if (d == c) return true;
return false;
[COLOR="#0000FF"]// can be replaced by[/COLOR]
return d==c;
[/CODE]

3/- Add an if(ValidBotBoot()) before the form.submit();
Copy linkTweet thisAlerts:
@littlebigmanauthorNov 29.2012 — Thanks for the help. I don't understand how to...

1. Remove the DrawBotBoot() function since it's in charge of displaying the question asked to the user

2. How to have jQuery validate the form, including the anti-SPAM question.

Does someone know of a simple anti-SPAM solution provided by jQuery that I could easily add to its form validator?
Copy linkTweet thisAlerts:
@007JulienNov 29.2012 — Sorry for 1/- I have nor seen the script in HTML !

3/- jQuery is not my cup of tea ! The second example of this jQuery page should solve the question

Adds a validation method that checks if a given value equals the addition of the two parameters.
[CODE]
jQuery.validator.addMethod("math", function(value, element, params) {
return this.optional(element) || value == params[0] + params[1];
}, jQuery.format("Please enter the correct value for {0} + {1}"));
[/CODE]
×

Success!

Help @littlebigman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...