PHP verification and check

Source: Internet
Author: User
PHP verification and check today, we are going to review a very important part of the web application development process. User input verification. This is the toughest part of any application. Why? Because developers cannot control it. You can write the best algorithm in the world, but if it contains user input, there is something wrong. Even if we put some coplicated logic to prevent incorrect symbol input for PHP verification and check
Today, we want to review a very important part of the web application development process. User input verification. This is the toughest part of any application. Why? Because developers cannot control it. You can write the best algorithm in the world, but if it contains user input, there is something wrong. Even if we put some coplicated logic to prevent incorrect symbol input, check data consistency, and do everything possible to ensure it is everything OK, it is still possible, the user entered an error number. Although they all indicate that we must try to avoid human errors 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 large number of books, they write, and even some programming languages, especially regular expressions. But today we just want to provide a brief introduction to the regular expression that can help us with user input. First, I suggest you familiarize yourself with some basic concepts of languages. Fully explain its syntax in the PHP Manual-> pattern syntax now, let's work. I will introduce some of the most common problems with user input. I'm sure you have seen most of them, if not all. We want to create a registration form for the required input domain. They are:-name-address-passport-email-phone number-zip code-date-user name-password here is the Test table (download), we must define some variables, we will use the PHP verification example and will hold our error message. Their values, we refresh the page every time it is cleared.
$ ErrName = "";
$ ErrAddress = "";
$ ErrEmail = "";
$ ErrPassport = "";
$ ErrPhone = "";
$ ErrZip = "";
$ ErrDate = "";
$ ErrUser = "";
$ ErrPass = "";

There are two methods to use regular expressions in PHP. One is the real PHP style. in this case, we must use the ereg () function, and the other is the Perl-style syntax, which 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 supports the most common regular expression syntax. It also provides us with more features that we can use. We will start verification with the user name. Only letters, spaces, and dashes are allowed. Therefore, we create our regexp (regular expression ). We will do our best to set a possible value. When a class is created, we include some symbols in parences. This is our class: [A-ZA-Z-our class contains all letters AZ (all lower-case letters), letters (upper-case letters), space and dash, now we need to set this class to apply for every character, and we will enter. Therefore, we have added a (+) and added our class definition after signing. What we still lack. We have not defined the scope of our verification test. We want to set part of the text and we are verifying it. If we do not do this, if we find that in our entry, this is a character we do not use, or even a game, will satisfy our regular expression. How can we achieve this? We set the start and end character strings between/^ $. "^" Indicates the beginning of the row and "$" indicates the end of the row. We are going to build our regexp. /^ [A-ZA-Z-] + $/slash preg_match is used to define the start and end of our regular expression, now we have done, what about us? One thing you just did. We define the method of our class and allow users to enter the name begining. This is what we want to prevent. Therefore, we must add some of our regexp, so sorting is not allowed. we define it as the first letter of a new user name. It can only contain uppercase letters. Now we can get the final result with the work we have done. If no matching preg_match () is returned, 0 is returned. In this case, we need 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 be from letters, dashes, spaces and must not start with dash

';

Let's move forward to the valitaion field, which will be the address. This is not much done here, because it can contain a large number of symbols. We only need to define a class to hold them. /^ [A-ZA-Z0-9, _: '] + $/translate the regexp: Don't get discouraged, check from the beginning to the end of the address string if our character is below AZ, including AZ, 0-9, space, underline, broken number, dot, comma, semicolon, double and firm quotation. You can add any character, which you think may be part of an address. Note that when we have quotation marks, we must put an escape character in front of them.

// Address must be word characters only
If (preg_match ("/^ [a-zA-Z0-9 _-.,:" '] + $/", $ _ POST [" address "]) = 0)
$ ErrAddress =' Address must be only letters, numbers or one of the following _-.,:"'

';

Our next task is to create a regular expression to verify the email. Here we will include another expression of the future, which is the CONSTANS and represend of the predefined class. Here, we will use the: W = [0-9A-ZA-Z _] class, including numbers, letters and underscores. D = [0-9] class only contains numbers. these constants save a lot of input and make the source code easier to read and understand. What is an email mask? The username in the first part can contain letters, numbers, and underlines. It starts with a letter. if we have a bit, it must be followed by a letter. Then, it must follow the @ symbol and the first part of the second. At the end, we must have two to four letters. Every time we have a character with a special meaning in regexp, we must use it as a character and escape it with a backslash.

// Email mask
If (preg_match ("/^ [a-zA-Z] w + (. w +) * @ w + (. [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)

';

The verification string is a passport. It can only contain digits, which are 10 or 12 digits. However, how do we set the number of characters required. We set the required number of characteras parences {} and regular expression to look like this/^ D {10} $/^ D {12}/USD. How can we combine these two words to allow us to use one or more words. We use. It indicates "| ". Our statement is complete/^ D {10 }$ | ^ D {12} $ /.

// Passport must be only digits

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.