简体   繁体   English

防止机器人提交表单的JavaScript代码

[英]javascript code to prevent bots from submitting form

I need a javascript code to prevents bots from submitting forms. 我需要一个JavaScript代码来防止漫游器提交表单。 But i need a client side code in javascript that work like CAPTCHA but don't call the server 但是我需要javascript中的客户端代码,其工作方式类似于CAPTCHA,但不要调用服务器

thank you 谢谢

Most straight forward and simple way will be to add or edit form data on the fly when the button is actually clicked: 最简单直接的方法是在实际单击按钮时即时添加或编辑表单数据:

<input type="hidden" name="SubmittedByHuman" value="NO" />
<input type="submit" value="Submit me" onclick="this.form.elements['SubmittedByHuman'] = 'YES';" />

Having this, on the server side check the value of form element called "SubmittedByHuman" - if it will be "NO" it means something bypassed the submit button - or as people mentioned correctly in comments, user did click but has disabled JavaScript. 有了这个,在服务器端检查名为“ SubmittedByHuman”的表单元素的值-如果它将为“ NO”,则表示某些内容绕过了提交按钮-或正如人们在注释中正确提到的那样,用户确实单击了但禁用了JavaScript。

do something like 做类似的事情

<h1>Type the result in the input box : 1+1</h1>

<input id="sum" type="text"/>

and before submitting you check if the value in the input is 2 and then submit it. 在提交之前,请检查输入中的值是否为2,然后提交。

To improve this type of code you could randomly create these 2 values in the h1 and save them into a var and before submiting check if input and sum are the same. 为了改进这种类型的代码,您可以在h1中随机创建这两个值,并将它们保存到var中,然后在提交输入和总和是否相同之前进行检查。

I doubt this is possible, as bots are sophisticated enough to bypass most things. 我怀疑这是否可能,因为机器人足够复杂,可以绕过大多数东西。

Remember, the bot isn't going to open the webpage in a browser and press submit. 请记住,该机器人不会在浏览器中打开网页并按提交。 It'll probably scan the page for a <form> , make a list of all the <input> fields, and perform a POST request containing all the data for each one. 它可能会在页面上扫描<form> ,列出所有<input>字段,然后执行POST请求,其中包含每个<form>的所有数据。

It won't run any javascript, or press any buttons. 它不会运行任何JavaScript或按任何按钮。 You'll have to make the check server-side. 您必须在服务器端进行检查。

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

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