简体   繁体   English

如何在联系表单中阻止文本区域上的 URL?

[英]How to block URL's on textarea in a contact form?

I'm receiving some unwanted links though the contact form, how to restrict the user or bot to enter URL link on the textarea?我通过联系表单收到一些不需要的链接,如何限制用户或机器人在文本区域输入 URL 链接?

I have done so many google searches but none of them worked for me, tried some libraries too like jquery validation but I couldn't find the solution for this.我做了很多谷歌搜索,但没有一个对我有用,也尝试了一些库,比如 jquery 验证,但我找不到解决方案。

You could simply have a list of "banned" URLs and check if any are included in the value of the textarea.您可以简单地拥有一个“禁止”URL 列表,并检查 textarea 的值中是否包含任何 URL。

 const textarea = document.getElementById('textarea'); const errorP = document.getElementById('error'); const bannedUrls = [ 'google.com', 'reddit.com', 'stackoverflow.com', ]; textarea.addEventListener('input', () => { const bannedUrlsInInput = bannedUrls.filter(url => textarea.value.includes(url)); if (bannedUrlsInInput.length > 0) { errorP.textContent = `${bannedUrlsInInput.join(', ')} not allowed`; } else { errorP.textContent = ''; } });
 <textarea id="textarea" rows="10" cols="80"></textarea> <p id="error"></p>

Note that this is not the most performant way.请注意,这不是最高效的方式。 If you have thousands of banned domains, you should consider using the exotic Bloom filter data structure.如果您有数千个被禁止的域,您应该考虑使用奇异的 Bloom 过滤器数据结构。

And last but not least -- if this validation is truly critical, it is not enough to just do client side validation, you need some server validation too.最后但并非最不重要的 - 如果此验证真的很重要,仅进行客户端验证是不够的,您还需要一些服务器验证。

You can use preg_match您可以使用 preg_match

if($_POST){
 !preg_match(^(https?:\/\/)?(www\.)?\w+\.[a-z]{2,6}(\/)?$, $_POST);
 $error = 'links are not allowed';
return $error;
}

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

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