織夢自定義表單提交為了防止用戶惡意提交給大家分下一下解決方案

  • 找到文件plus/diy.php,大概在40行左右搜索:

$dede_fields = empty($dede_fields) ? '' : trim($dede_fields);

  • 在后面加上以下代碼:
//增加必填字段判斷
if($required!=''){
if(preg_match('/,/', $required))
{
$requireds = explode(',',$required);
foreach($requireds as $field){
if($$field==''){
showMsg('帶*號的為必填內(nèi)容,請完善您的信息!', '-1');
exit();
}
}
}else{
if($required==''){
showMsg('帶*號的為必填內(nèi)容,請完善您的信息!', '-1');
exit();
}
}
}
//end
 
  • 在你的表單的中找到
<form action="/plus/diy.php" enctype="multipart/form-data" method="post">
  • 在下面加上下面代碼
<input type="hidden" name="required" value="數(shù)據(jù)字段名1,數(shù)據(jù)字段名2,數(shù)據(jù)字段名3" /> 如:

<input type="hidden" name="required" value="czdwmc,xm,sj,ppmc,kd,hhbzzw,ptbzzw" />

用JS判斷
紅色部分為自定義字段,把以下代碼放在</head>之前,
<script type='text/javascript'>
<!-- 
$(document).ready(function() 
{ 
//驗證 
$('#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("標題不能為空!"); 
return false; 
} 
if($('#text').val()=="") 
{ 
$('#text').focus(); 
alert("具體內(nèi)容不能為空!"); 
return false; 
} 
}) 
}); 
-->
</script>

 上面只能判斷數(shù)值是否為空,結(jié)合正則表達式,能準確判斷輸入的表單信息是否為規(guī)定格式數(shù)據(jù)。舉例說明:

<script type="text/javascript">
  $(function(){
  $(".btn").click(function(){
  var myNum=/^[\u4e00-\u9fa5]+$/;
 if(myNum.test($("#name").val())){}else{alert("請輸入中文名字");return false;};
  var myNum=/^[\u4e00-\u9fa5]+$/;
 if(myNum.test($("#xq").val())){}else{alert("請輸入小區(qū)中文名字");return false;};
  var mymj=/^([1-9][0-9]{1,3})+(.[0-9]{1,4})?$/
 if(mymj.test($("#mj").val())){}
 else{alert("戶型面積請輸入首位不為零并且在2-4位的有效數(shù)字有效數(shù)字");return false;};
  var myphone=/^\d{8,11}$/
 if(myphone.test($("#phone").val())){}
 else{alert("電話位數(shù)在8-11位");return false;};
 if($("input[type=text]").val()==''){alert("文本框不能為空!");return false;}
 else {alert("發(fā)布成功!請保持電話暢通!客服人員會在24小時之內(nèi)與您聯(lián)系!");return true;}});});
</script>