分类分类
更新时间:2026-10-06 22:23:17作者:fang
织梦dedecms自定义表单用的最多的就是制作留言板,报名等功能,但是添加的字段不填写就能提交,容易被恶意提交,为了防止这些,我们可以把这些字段选项设定为必填项。
方法一:PHP方法
1. 打开 plus/diy.php 文件
找到
$dede_fields = empty($dede_fields) ? '' : trim($dede_fields);
在其下边添加如下代码
//增加必填字段判断
if($required!=''){
if(preg_match('/,/', $required))
{
$requireds = explode(',',$required);
foreach($requireds as $field){
if($field==''){
showMsg('带*号的为必填内容,请正确填写', '-1');
exit();
}
}
}else{
if($required==''){
showMsg('带*号的为必填内容,请正确填写', '-1');
exit();
}
}
}
//end
2.在模版的表单里加<input type="hidden" name="required" value="name,sex" />其中 value 就是必须字段,多个用“,”隔开。
示例:
<form action="/plus/diy.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="required" value="name,sex" />
<input type="hidden" name="action" value="post" />
<input type="hidden" name="diyid" value="1" />
<input type="hidden" name="do" value="2" />
<table style="width:97%;" cellpadding="0" cellspacing="1">
<tr>
<td align="right" valign="top">姓名:</td>
<td><input type='text' name='name' id='name' style='width:250px' class='intxt' value='' />
</td>
</tr>
<tr>
<td align="right" valign="top">性别:</td>
<td><select name='sex' style='width:50px'><option value='男'>男</option>
<option value='女'>女</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="top">电话:</td>
<td><input type='text' name='tell' id='tell' style='width:250px' class='intxt' value='' />
</td>
</tr>
<tr>
<td align="right" valign="top">地址:</td>
<td><input type='text' name='add' id='add' style='width:250px' class='intxt' value='' />
</td>
</tr>
<tr>
<td align="right" valign="top">备注内容:</td>
<td><input type='text' name='con' id='con' style='width:250px; height:100px' class='intxt' value='' />
</td>
</tr>
<input type="hidden" name="dede_fields" value="name2,text;sex2,select;tell2,text;add,text;con2,text" />
<input type="hidden" name="dede_fieldshash" value="78764e448024ba3607705cbf961ebf3f" /></table>
<div align='center' style='height:30px;padding-top:10px; padding-left:130px'>
<input type="submit" name="submit" value="提 交" class='coolbg' />
<input type="reset" name="reset" value="重 置" class='coolbg' />
</div>
</form>
方法二:JS方法
1、在要发布表单的模板中添加如下代码
<script src='你的路径/yanzheng.js' type="text/javascript"></script>
2、在你自定义的路径中新建yanzheng.js文件,然后复制以下内容粘贴保存
<!--
$(document).ready(function()
{
//验证
$('#complain').submit(function ()
{
if($('#name').val()==""){
$('#name').focus();
alert("用户名不能为空!");
return false;
}
if($('#tel').val()=="")
{
$('#tel').focus();
alert("联系电话不能为空!");
return false;
}
if($('#title').val()=="")
{
$('#title').focus();
alert("标题不能为空!");
return false;
}
if($('#text').val()=="")
{
$('#text').focus();
alert("具体内容不能为空!");
return false;
}
})
});
-->
注意:
$('#complain').submit(function () //complain为自定义表单的ID,如果生成的表单没有可以自行加上,即 id="complain".
if($('#name').val()==""){
$('#name').focus();//#name为要验证表单中的ID,如想让用户名不能为空,在后台用户名的数据字段名设为name,下同.
以上就是本文章的内容,希望对大家有所帮助
相关
摄像头远程监控看家2026官方最新版本生活服务36.4 MBv1.0.02026-10-06
下载公务员省考最新手机版学习办公57.5 MBv6.09.112026-10-06
下载野性蜂巢(Wild Hive)经营养成267.49 Mv1.66.42026-10-04
下载趣好卖app网上购物74.76 Mv5.2.52026-10-04
下载兔叽月记app医疗健康55.33 Mv1.5.22026-10-04
下载魔力弹神策略游戏33.56 Mv1.0.02026-10-04
下载大破坏幸存者策略游戏62.28 Mv1.0.32026-10-04
下载啃豆小仓拼豆app实用工具41.74 Mv1.32026-10-04
下载方块公国安卓移植版策略游戏1.58Gv1.2.202026-10-04
下载快车顺风车出行2026官方最新版本生活服务82.0 MBv1.1.22026-10-04
下载网约上门按摩2026官方最新版本生活服务73.9 MBv2.0.22026-10-04
下载道运考试通(道路考练工具)学习办公32.8 MBv6.0.12026-10-04
下载










