Home > CMS Tutorial > DEDECMS > body text

How to set required fields in custom forms in dedecms

藏色散人
Release: 2019-12-16 10:18:11
Original
2307 people have browsed it

How to set required fields in custom forms in dedecms

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);
Copy after login

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
Copy after login

4.After saving, find this line of code on the form page:

The code is as follows:

Copy after login

Here Below the line of code, add the code:

Copy the code as follows:

Copy after login

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:

Copy after login

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:

Copy after login

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:

Copy after login

Note:

The code is as follows:

$('#complain').submit(function () //complain为自定义表单的ID,如果生成的表单没有可以自行加上,即 id="complain".
if($('#name').val()==""){
$('#name').focus();//#name为要验证表单中的ID,如想让用户名不能为空,在后台用户名的数据字段名设为name,下同.
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!