


How to use PHP regular expression to validate form data? (detailed steps)
The previous article introduced to you "How to use regular replacement to clear all HTML tags in a string? 》, this article continues to introduce to you how to use PHP regular expressions to verify form data?
#How to use PHP regular expressions to validate form data?
First, we lay out our HTML form, and then add the form tag (note: all user-related content must be transmitted using post), and then, we Add the account number, password and email address to the form, and finally add a login button.
Take the code demonstration as an example:
<?php /*******用户数据验证*********/ ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户信息验证</title> </head> <body> <form action="" method=" post"> 账号: <input type="text" name="name" value="" /><br/> 密码:<!--密码一定用type=password 这里没用的原因是为了让大家看到输入的内容--> <input type= "text" name="pwd" value="" /><br/> 邮箱: <input type= "text" name=" email" value="" /><br/> <input type="submit" value="登录" > </form>
The code results are as follows;
The operation result is successful, but the case we gave shows that the account number needs to be composed of letters, numbers and underscores. The password needs to be composed of 8-16 characters of uppercase and lowercase letters and numbers. The email address needs to enter the correct email address. Therefore, we add rules that need to be added by default in the code. The code demonstration is as follows;
<?php /*******用户数据验证*********/ $userInfo = '字母数字以及下划线组成'; $pwdInfo = '6-13位大小写字母以及数字'; $emailInfo = '输入正确的email地址' ; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户信息验证</title> </head> <body> <form action="" method=" post"> 账号: <input type="text" name="name" value="" /><?=$userInfo?><br/> 密码:<!--密码一定用type=password 这里没用的原因是为了让大家看到输入的内容--> <input type= "text" name="pwd" value="" /><?=$pwdInfo?><br/> 邮箱: <input type= "text" name=" email" value="" /><?=$emailInfo?><br/> <input type="submit" value="登录" > </form>
The code results are as follows As shown;
According to the code display, the content we need is also displayed in the running results, but after we get the default display, we need to define the verification rules. Verification Whether the content output by the user meets the requirements; for example, we take the user as an example, first define $userpattern = '/^\w $/S'; and then make a judgment and add an if conditional sentence. In the if statement, if it is not set The user will not be able to judge the conditional sentence, or will not enter if the setting is wrong, and so on. Let’s take the code as an example:
<?php /*******用户数据验证*********/ $userInfo = '字母数字以及下划线组成'; $pwdInfo = '6-13位大小写字母以及数字'; $emailInfo = '输入正确的email地址' ; $userPattern ='/^\w+$/S'; if(isset($_POST['name'])){ if(preg_match($userPattern,$_POST['name'])){ $userInfo = '<font color="green">正确</font>'; }else{ $userInfo = '<fort color= "pick">字母数字以及下划线组成</font>'; } $pwdPattern ='/^[a-zA-Z0-9]{6,13}$/S'; if(isset($_POST['pwd'])){ if(preg_match($pwdPattern,$_POST['pwd'])){ $pwdInfo ='<font color=" green">正确</font>'; }else{ $pwdInfo = '<fort color= "pick">6-13位大小写字母以及数字</font>'; } } $emailPattern = '/^\w+@[0-9a-zA-Z]+(\.[a-zA-Z]{2,5})+$/S'; if(isset($_POST['email'])){ if (preg_match( $emailPattern,$_POST['email'])){ $emailInfo = '<font color=" green">正确</font>' ; }else{ $emailInfo = '<font color="red" >输入正确的email地址</font>'; } } ?>
The code results are as follows;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use PHP regular expression to validate form data? (detailed steps). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Nowadays, in the era of digitalization and networking, security has become one of the important factors that cannot be ignored in the Internet world. Especially in business scenarios with high data sensitivity, how to improve the security of websites, applications and user data is particularly important. Using two-step authentication in PHP forms to enhance security is a feasible solution. Two-Factor Authentication (2FA), also known as double authentication and multi-factor authentication, refers to the process where the user completes the regular account password.

When using PHP forms for data submission, the problem of repeated form submission often occurs. This can lead to inaccurate data or, worse, system crashes. Therefore, it is very important to understand how to prevent duplicate submissions. In this article, I will introduce some PHP form protection techniques to help you effectively prevent repeated form submission problems. 1. Add a token to the form Adding a token to the form is a common way to prevent repeated submissions. The principle of this method is to add a hidden field to the form, which contains

With the continuous development of network technology, security issues have increasingly become an issue that cannot be ignored in network application development. Among them, the cross-site request forgery (CSRF) attack is a common attack method. Its main purpose is to use the user to initiate an illegal request to the background by allowing the user to initiate a malicious request in the browser when the user is logged in to the website. This leads to server-side security vulnerabilities. In PHP applications, using form validation is an effective means of preventing CSRF attacks. Add CSRFToken to verify CSRF attacks

How to deal with mobile and responsive design in PHP forms. With the popularity and frequency of mobile devices increasing, and more and more users using mobile devices to access websites, adapting to mobile has become an important issue. When dealing with PHP forms, we need to consider how to achieve a mobile-friendly interface and responsive design. This article explains how to handle mobile and responsive design in PHP forms and provides code examples. 1. Responsive forms using HTML5 HTML5 provides some new features that can easily implement responsive forms.

How to handle multi-language input in PHP forms With the development of globalization, multi-language support for websites has become a necessary feature. In PHP development, how to handle multi-language input so that users can enter data in different languages in the form and store and display the data correctly is an important issue that developers need to solve. This article will introduce how to use PHP to handle multi-language input and give corresponding code examples. 1. Set the language setting of the website In the PHP code, we can use setlocale

With the rapid development of Internet technology, more and more websites use PHP forms to collect user information. However, the risks that come with it are getting higher and higher, because hackers may use the form to capture users' private information or conduct malicious attacks. In order to prevent these risks, we need to add user operation records to PHP forms to improve security. 1. What is user operation record? User operation record is a record of all operations performed by each user when using the system, including logging in, registering, filling out forms, submitting forms, etc. These records can be used to track

In this digital age, data security has become increasingly important. Whether you are an individual user or a business user, you need to consider how to protect your data from attacks. PHP forms are commonly used tools for collecting data, but many people don’t know how to add data desensitization to forms to improve the security of form data. This article will explain how to add data desensitization to PHP forms to improve the security capabilities of form data. What is data desensitization? Simply put, data desensitization is to encrypt or replace sensitive data so that the original

With the continuous development of the Internet, forms have become one of the functions we use on our daily websites. Getting users to fill out a form is undoubtedly a tedious task, so it’s necessary to use some tricks to simplify the process. This article will introduce techniques for implementing form autofill in PHP. 1. Use default value When setting the default value of the form, you can use the "value" attribute in the form tag to specify it. Here is an example: <inputtype="text"name=&q
