SHOPNC Study Note Three: form data validation

Source: Internet
Author: User

Data security is a key point in the development of the program, especially for the mall program, the general form of data validation is the use of front-end JS and the back-end PHP verification combination of the way, SHOPNC Mall system in the form of data validation is the same, The background PHP verification of SHOPNC system is divided into two parts: Controller layer Verification and framework underlying verification, the function of validate verification class is to complete the verification in the Controller.

Here is a section of logging in using the Validate class validator:

if (Chksubmit ()) {

$obj _validate = new Validate ();

$obj _validate->validateparam = Array (

Array ("Input" =>$_post["username"], "require" and "true", "message" = "Please enter username"),

Array ("Input" =>$_post["password"], "require" and "true", "message" = "Please enter password"),

Array ("Input" =>$_post["Captcha"], "require" = (C (' Captcha_status_login ')? "True": "false"), "message" = "Please enter a verification code"),

);

$error = $obj _validate->validate ();

if ($error! = ") {

Incorrect input information, validation failed!

Showvalidateerror ($error);

}else {

/**

* Program continues to execute down

*/

}

}

Code Analysis:

First, the Chksubmit function to determine whether the form submission is legitimate, the token is correct, after the judgment passed, using the Validate class for data validation, validate class of the Validateparam attribute array to hold the information to be verified, where input is the data to be verified, Require is a required entry (True/false), and the message returns a hint if the validation failed. It can be seen that the user name and password for the above example are required, and the verification code depends on the value of the system switch captcha_status_login. The C function is to obtain the system configuration item.

When the preparation is finished, the Validate method of invoking the class is validated, and if the validation is not over, an error message is returned, otherwise null is returned. The output of the error message is done by the Showvalidateerror function, which returns a different hint form than whether it is an asynchronous commit.

In addition to verifying that it is required, the Validate class can also perform some common data type validation. such as verification of mail, telephone, URL, currency, postal code, QQ, numbers, English, Chinese characters, two numerical comparisons. Such as:

$obj _validate = new Validate ();

$obj _validate->validateparam = Array (

Array ("Input" =>$_post["Password1"], "require" and "true", "validator" = "compare", "operator" and "= =", "to" = >$_post["Password2"], "message" = "Two password input consistent"),

Array ("Input" =>$_post["input0"], "require" and "true", "validator" = "email", "message" + "message verification",

Array ("Input" =>$_post["INPUT1"], "require" and "true", "validator" + "phone", "message" = "Fixed verification"),

Array ("Input" =>$_post["Input2"], "require" and "true", "validator" and "mobile", "message" + "Phone Verification"

Array ("Input" =>$_post["INPUT3"], "require" and "true", "validator" = "url", "message" + "URL validation",

Array ("Input" =>$_post["Input4"], "require" and "true", "validator" = "Currency", "message" and "Price verification",

Array ("Input" =>$_post["INPUT5"], "require" and "true", "validator" and "number", "message" + ' digital verification '),

Array ("Input" =>$_post["INPUT6"], "require" and "true", "validator" and "Zip", "message" = "zip verification"),

Array ("Input" =>$_post["INPUT6"], "require" and "true", "validator" = "QQ", "message" = "QQ Verification"

Array ("Input" =>$_post["INPUT6"], "require" and "true", "validator" and "English", "message" and "Letter verification"

Array ("Input" =>$_post["INPUT6"], "require" and "true", "validator" and "Chinese", "message" = "Chinese Validation"),

);

$error = $obj _validate->validate ();

SHOPNC Study Note Three: form data validation

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.