織夢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,下同.
此文由 網站目錄_網站網址收錄與提交入口 編輯,未經允許不得轉載!: