How to set the required fields for custom forms in dedecms?
The example in this article describes how to set the required fields of the custom form in dedecms. Share it with everyone for your reference. The specific implementation method is as follows:
Recommended learning: 梦Weavercms
Let me talk about it first, it has been tried and confirmed to be effective:
1. Open the editor \plus\diy.php
2. Find this line of code around line 40:
$dede_fields = empty($dede_fields) ? '' : trim($dede_fields);
3. Under this line of code, add the code. If copied, delete the spaces in the code. Line.
The code is as follows:
//增加必填字段判断 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
4.After saving, find this line of code on the form page:
The code is as follows:
<form action="/plus/diy.php" enctype="multipart/form-data" method="post">
Here Below the line of code, add the code:
Copy the code as follows:
<input type="hidden" name="required" value="数据字段名,数据字段名" />
Note that this line of code needs to be modified according to the required fields that your form needs to set, such as setting "name" ", "Email" is required.
Add a new field--"Form prompt text":Name--"Field name":name
Add a new field--"Form prompt Text": Email--"Field Name": email
This line of code should be:
The code is as follows:
<input type="hidden" name="required" value="name,email" />
In this way, these two options are set to required After filling in the fields, if you submit without filling them in, a window will open prompting "The fields marked with * are required, please fill them in correctly." Of course, this sentence can be changed to other words.
See it online Another method, not tested, just as a data collection.
js method:
1. First add
code to the template of the form to be published as follows:
<script src='你的路径/js.js' type="text/javascript"></script>
2. Create a new file js.js in your customized path, then copy and paste the following content and save it. Code:
The code is as follows:
<!-- $(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; } }) }); -->
Note:
The code is as follows:
$('#complain').submit(function () //complain为自定义表单的ID,如果生成的表单没有可以自行加上,即 id="complain". if($('#name').val()==""){ $('#name').focus();//#name为要验证表单中的ID,如想让用户名不能为空,在后台用户名的数据字段名设为name,下同.
3. After setting it up, you can see the effect by updating it.
The above is the detailed content of How to set required fields in custom forms in dedecms. For more information, please follow other related articles on the PHP Chinese website!