简体   繁体   English

表单上JavaScript验证的数学和

[英]Maths sum for javascript validation on form

Trying to create a very piece of validation to prevent spammers. 尝试创建一个非常有效的验证以防止垃圾邮件发送者。 I want a field with a label saying 'What's 2 + 7?' 我想要一个标签为“ 2 + 7是什么?”的字段 so the Javascript needs to know the result and pass the validation. 因此Javascript需要知道结果并通过验证。 I'm struggling to write this, would it be something along the lines of: 我正在努力写这篇文章,难道是这样的:

var valid = '';
var required =  ' is required';
var sum = $('form #sum').val();

if (sum == '9') {
    valid += '<p>An answer is' + required + '</p>';
}

Client side validation via Javascript is not going to do much to guard against spammers. 通过Javascript进行的客户端验证在防止垃圾邮件发送者方面无济于事。 Most spammers will have bots that don't even parse Javascript. 大多数垃圾邮件发送者都会拥有甚至不解析Java脚本的漫游器。

The only way to do this reliably is to handle it server side. 可靠地执行此操作的唯一方法是在服务器端进行处理。

I would recommend trying something like Recaptcha: http://www.google.com/recaptcha 我建议尝试类似Recaptcha的方法: http : //www.google.com/recaptcha

It is hard to tell exactly what's needed without looking at more code. 如果不查看更多代码,很难确切说明需要什么。

A simple question like this may very well work if it is generated server side and it is a lot better for accessibility than captchas. 如果它是在服务器端生成的,那么类似这样的简单问题可能会非常有用,并且可访问性要比验证码好得多。 In order to be effective the question and answer should also be randomly selected. 为了有效,问题和答案也应随机选择。

You may chose to include a pre-submit client side validation for usability purposes, but since spam bots ignore JavaScript, it is useless to prevent them, unless you do something really complicated. 出于可用性的考虑,您可能选择包括提交前客户端验证,但是由于垃圾邮件机器人会忽略JavaScript,因此除非有真正复杂的操作,否则阻止它们是无用的。

A bonus tip off topic: Your JQuery selector is inefficient. 一个额外的提示主题:您的JQuery选择器效率低下。 When using an id-selector you will never need to include its parent. 使用id选择器时,您将不需要包括其父项。

Ok so this is not going to answer your question per se but I want to suggest that you don't do this. 好的,这本身不会回答您的问题,但是我建议您不要这样做。 Creating these spam preventions mechanisms is actually very complicated and your idea isn't really going to work if a spammer really wants to come after your site. 创建这些防止垃圾邮件的机制实际上非常复杂,如果垃圾邮件制造者真的想追随您的网站,那么您的想法就行不通了。 I would suggest you look into a service called recaptcha . 我建议您研究一下称为recaptcha的服务。 It is free, easy to set up and the information entered by users is put to use digitizing old books. 它是免费的,易于设置,并且用户输入的信息可用于数字化旧书。

As I wrote in comments, you could prevent the bots from posting your data with just a javascript by itself. 正如我在评论中所写,您可以阻止bot本身仅使用javascript发布数据。 Let me explain how you could do it: 让我解释一下您该怎么做:

  1. You could set initially the wrong or empty action for your form and later, with the help of javascript, set it to the correct one if the answer on the 'sum' was correct. 您可以先为表单设置错误或为空的操作,然后在javascript的帮助下,如果“ sum”的答案正确,则将其设置为正确的操作。 Or even without the request for 'sum' as most of the spam-bots do not run js. 甚至不需要“ sum”,因为大多数垃圾邮件机器人都不会运行js。

  2. You could insert a hidden field named 'passed' with false initial value of it. 您可以插入一个名为false的隐藏字段,其初始值为false Again, based on form interaction, you could set it to true and later check the data from the form (the value of this hidden field) on the server. 同样,基于表单交互,您可以将其设置为true,然后在服务器上检查表单中的数据(此隐藏字段的值)。

  3. The last method, which I prefer in most cases, is to encode html code of your form with, for example, base64 and use your javascript to convert it back to HTML code. 在大多数情况下,我更喜欢使用最后一种方法,例如使用base64对表单的html代码进行编码,然后使用javascript将其转换回HTML代码。 As soon as bots do not run js, they will not even know that you have a form on the page. 一旦机器人不运行js,他们甚至都不知道页面上有表单。 The good part here is that you do not have to ask a person to enter something else in the form. 这里的好处是您不必要求别人输入表格中的其他内容。

All these methods can be bypassed by a person interested in spamming on your website. 有兴趣在您的网站上发送垃圾邮件的人可以绕过所有这些方法。 He could check the final data sent to the server and create the set of the same requests to your server. 他可以检查发送到服务器的最终数据,并为您的服务器创建一组相同的请求。 That is why you need some server-side support in order to prevent you form even from manually crafted spam requests. 这就是为什么您需要一些服务器端支持以防止您通过手工制作的垃圾邮件请求来形成表格的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM