首页 期权学习期权知识正文

纯js实现Emlog评论添加验证码防止垃圾评论

xiaojiucai 期权知识 2020-08-18 455 0

使用emlog博的朋友大部分可能经常遇到一些垃圾评论,以下教程以默认模板为例 

第一步、在全局js中加入如下代码:里面的一些正则可以自行替换成你想要的

function SLyz(){
if(document.commentform.comname.value.length == 0){alert("请输入昵称");document.commentform.comname.focus();return false;}
var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;if(!reg.test(document.commentform.commail.value)){alert("1、邮箱地址是否填写 \r\n2 、邮箱地址是否正确");document.commentform.commail.focus();return false;}
var reg = /^(http:\/\/|https:\/\/)([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;if(!reg.test(document.commentform.comurl.value) && (document.commentform.comurl.value)){alert("网址格式必须加http://或https://且网址结尾不能有“/”;次选为选填项,可不填写");document.commentform.comurl.focus();return false;}
if(document.commentform.comment.value.length == 0){alert("请填写评论内容且评论内容需包含中文");document.commentform.comment.focus();return false;}
}

第二步、在模板文件module.php中找到代码

<form method="post" name="commentform" action="<?php echo BLOG_URL; ?>index.php?action=addcom" id="commentform">

修改为下面代码

<form method="post" name="commentform" action="<?php echo BLOG_URL; ?>index.php?action=addcom" id="commentform" onSubmit="return SLyz();">

以上2步就完成了

原文链接:https://www.qiquanji.com/post/7175.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论