国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

dedecms教程:自定義表單字段為必填項(xiàng)的方法

2024-04-25 20:44:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

織夢(mèng)dedecms自定義表單用的最多的就是制作留言板,報(bào)名等功能,但是添加的字段不填寫就能提交,容易被惡意提交,為了防止這些,我們可以把這些字段選項(xiàng)設(shè)定為必填項(xiàng)。

方法一:PHP方法

1. 打開(kāi) 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('帶*號(hào)的為必填內(nèi)容,請(qǐng)正確填寫', '-1');

exit();

}

}

}else{

if($required==''){

showMsg('帶*號(hào)的為必填內(nèi)容,請(qǐng)正確填寫', '-1');

exit();

}

}

}

//end

2.在模版的表單里加<input type="hidden" name="required" value="name,sex" />其中 value 就是必須字段,多個(gè)用“,”隔開(kāi)。

示例:

<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">備注內(nèi)容:</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、在要發(fā)布表單的模板中添加如下代碼

<script src='你的路徑/yanzheng.js' type="text/javascript"></script>

2、在你自定義的路徑中新建yanzheng.js文件,然后復(fù)制以下內(nèi)容粘貼保存

<!--

$(document).ready(function()

{

//驗(yàn)證

$('#complain').submit(function ()

{

if($('#name').val()==""){

$('#name').focus();

alert("用戶名不能為空!");

return false;

}

if($('#tel').val()=="")

{

$('#tel').focus();

alert("聯(lián)系電話不能為空!");

return false;

}

if($('#title').val()=="")

{

$('#title').focus();

alert("標(biāo)題不能為空!");

return false;

}

if($('#text').val()=="")

{

$('#text').focus();

alert("具體內(nèi)容不能為空!");

return false;

}

})

});

-->

注意:

$('#complain').submit(function () //complain為自定義表單的ID,如果生成的表單沒(méi)有可以自行加上,即 id="complain".

if($('#name').val()==""){

$('#name').focus();//#name為要驗(yàn)證表單中的ID,如想讓用戶名不能為空,在后臺(tái)用戶名的數(shù)據(jù)字段名設(shè)為name,下同.

以上就是本文章的內(nèi)容,希望對(duì)大家有所幫助

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 景宁| 尉犁县| 长垣县| 蛟河市| 汉源县| 四川省| 宜丰县| 疏勒县| 桓仁| 平度市| 黄浦区| 保德县| 阳山县| 共和县| 北辰区| 德惠市| 社会| 沧源| 雅江县| 永定县| 迁安市| 永州市| 平安县| 三河市| 汝阳县| 农安县| 木兰县| 民权县| 慈溪市| 静安区| 鄂温| 高阳县| 达尔| 新宁县| 横山县| 新安县| 竹山县| 会宁县| 乐陵市| 泽普县| 炉霍县|