分类分类
更新时间:2026-05-11 02:03:14作者: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,下同.
以上就是本文章的内容,希望对大家有所帮助
相关
修真情缘角色扮演303.33 MBv1.0.02026-05-10
下载我的小独角兽女孩休闲益智380.89 MBv1.0.232026-05-10
下载超级种田男孩手机版经营养成1.31Gv1.0.92026-05-10
下载不必要的实验冒险游戏64.9 MBv1.1.592026-05-10
下载军师联盟策略游戏163.2 MBv0.0.12026-05-10
下载翻天喜地凑大钱手机版休闲益智138.72 MBv1.0.92026-05-10
下载东离剑游纪手游动作射击1.6Gv1.4.22026-05-10
下载天猫养车商家版App学习办公74.95 MBv0.6.12026-05-10
下载掌上命运方舟App手游辅助137.48 MBv1.9.12026-05-10
下载退役军人服务APP生活服务67.16 MBv1.2.52026-05-10
下载屯漫漫画APP资讯阅读23.06 MBv2.0.32026-05-10
下载蛋仔派对蛋壳App手游辅助185.73 MBv0.0.22026-05-10
下载









