PHP Diseases and check

Source: Internet
Author: User
PHP Validation and inspection
Today, we want to review a very important part of the development process for Web applications. Validation of user input. This is one of the toughest parts of any application. Why is this? Because the developer can't control it. You can write the best algorithm in the world, but still if it contains user input there is a mistake somewhere. Even if we put some coplicated logic to prevent the input of the wrong symbols, check the consistency of the data and do everything possible to make sure it is all OK, it is still likely that the user entered the error number. Although it is said that we must try to avoid human error and the best way to do this is to use regular expressions. The basicly regular expression is used for string matching. They are all text strings based on search and pattern matching. A lot of books, they write, and even some programming language designs, especially regular expressions. But today we're just going to have a brief introduction to the regular expression that can help us with the user input. First, I suggest some basic concepts of the language you are familiar with. Fully explain its syntax in PHP manual-> mode syntax now, let's work on it. I'll cover some of the most common issues with user input. I'm sure you've seen most of them if not all. We want to create the registration form for the required input fields. They are as follows:-name-address-passport-email-phone-Postal code-date-username-Password Here is the test table (download), we must define some variables, we will use PHP to verify the example, will hold our error message. Their values, we refresh the page each time to be cleared.
$errName = "";
$errAddress = "";
$errEmail = "";
$errPassport = "";
$errPhone = "";
$errZip = "";
$errDate = "";
$errUser = "";
$errPass = "";

There are two ways to use regular expressions in PHP. One is the real PHP style, in which case we have to use the Ereg () function, and the other is to use Perl-style syntax that we verify. In this case, we must use the Preg_match () function. In this tutorial, we will use Preg_match (), because it is faster in most cases, and also supports the most common regular expression syntax. It also provides us with more features that we can use. We will begin validation with the user name. We will only allow letters, spaces and dashes. Therefore, we create our regexp (regular expression). We will do the one class for our possible values. When the class is created, we enclose some of the symbols in the parences. This is our class: [A-za-z-Our class contains all the letters AZ (all lowercase letters), letters (uppercase letters), spaces and dashes between, now we want to set this class to apply every character we enter. So we added a (+), plus signature after our class definition. We are still missing something. We have not yet defined the scope of our validation tests. We're going to set a part of the text that we're validating. If we do not, if found in our entry, this is the character we have not used, or even a match, will satisfy our regular expression. How do we do that? We put a string between us/^ $/start and end characters. "^" means the beginning of the line and "$" means the end of it. We are ready to build our regexp. /^ [a-za-z-] + $/Slash Preg_match used to define the beginning and end of our regular expression, and now we are done, what about us? There's just one thing to do. We define our class in a way that allows the user to enter the name begining dash. This is something we want to prevent. Therefore, we have to add some of our regexp, so we will not allow sorting, we define the first letter of a class of new user names. It can contain only uppercase letters. Now we combine the work we've done so far to get the final result. If no matching Preg_match () returns 0. In this case, we are going to set our error variable so that we can display some meaningful information to the user/^ [AZ] [a-za-z-] + $/

Full Name must contain letters, dashes and spaces only and must start with upper case letter.
if (Preg_match ("/^[a-z][a-za-z-]+$/", $_post["name"]) = = = 0)
$errName = '

Name must is from letters, dashes, spaces and must not start with dash

';

Let's move on to the next Valitaion field, which will be the address. There's not much to do here, because it can contain a lot of symbols. We just need to define a class to hold them. /^ [a-za-z0-9,_: "'] + $/Translation of the regexp: do not be discouraged, from the beginning to the end of the address string check if our character is the following AZ, including az,0-9, spaces, underscores, dashes, dots, commas, semicolons, double and firm quotes. You can add any character that you think may be part of an address. The thing to note here is that when we have quotes, we have to put an escape character in front of them.

Address must is word characters only
if (Preg_match ("/^[a-za-z0-9 _-.,:" ']+$/", $_post[" Address "]) = = = 0)
$errAddress = '

Address must is only letters, numbers or one of the following _-. , : " '

';

Our next task is to create a regular expression validation e-mail message. We are here to include another expression of the future, which is the constans,represend of the predefined classes. Here's a list of people we'll use: W = [0-9a-za-z_] class, including numbers, letters and underscores. D = [0-9] class consists of only numbers these constants save a lot of input and make the source code easier to read and understand. What is the mask of email? The first part of the user name can contain letters, numbers, dots, and underscore characters. It starts with a letter, and if we're a bit, it must be followed by a letter. Then, [email protected],[email protected]exp The special meaning of the character, we want to use it as a character, we must escape with a backslash.

Email Mask
if (Preg_match ("/^[a-za-z]w+ (. w+)" [Email protected]+ (. [ 0-9a-za-z]+) *. [A-za-z] {2,4}$/", $_post[" email "]) = = = 0)
$errEmail = '

Email must comply with this mask:chars (. chars) @chars (. chars). Chars (2-4)

';

Verify that the string is a passport. It can only contain numbers, which are 10-bit or 12-digit numbers. But how do we set the number of characters we want. We take the required number of Characteras Parences {} and regular expressions to look like this/^ D {Ten} $//^ D {12}/USD. How do we combine these two words to let us use one or the other. We use. Its logo is "|". Our statement is complete/^ D {10} $ | ^ D {12} $/.

Passport must is only digits
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.