The PHP code framework for form validation

Source: Internet
Author: User
Tags array empty eval implement php code strlen client

In my last article, I talked about using JavaScript to do integrated form validation, which is sufficient for client validation, but good form validation should be done both on the client and server side-that's what this article is about.

What should you write if you use a generic validation method?

if ($_post[' some '] does not meet the condition) {

Die ("wrong");

}

An integrated authentication method similar to JavaScript, the array and eval statements can also be used in PHP to implement the form validation (server-side) framework. General steps:

1, the user submitted data from the $_get or $_post array to the custom array (optional),

2, fill the validation array (including variable name, conditions and hints, etc.),

3, add a fixed validation code.

Let me give you an example, if you want to collect the user name and age from the Web page and implement data validation on the server side, then you may need form.html and act.php. The form.html contains the form:

<form Method=post action=act.php>

Name: <input type=text name= ' name ' ><br>

Age: <inupt type=text name= ' uage ' ><br>

<input type=submit value= "Submit" >

</form>

Validation of forms is implemented in act.php:

$userinfo = Array (); Step 1

$userinfo [' name '] = isset ($_post[' uname '])? $_post[' uname ']: ';

$userinfo [' age '] = isset ($_post[' uage '])? (int) $_post[' uage ']: 0;

$error _message = ""; Error message

Step 2

$elems = Array (
Array ("\ $userinfo [' name ']", ' strlen (#) <1 ', ' name cannot be empty! '),

If you omit step 1, rewrite as follows:

Array ("\$_post[' uname ']", ' strlen (#) <1 ', ' name cannot be empty! '),

Array ("\ $userinfo [' Age ']", ' #<=0 ', ' ages incorrect! ')

);

Step 3

for ($i = 0; $i < count ($elems); $i + +) {
if (eval ("Preg_replace" ("/[#]/", $elems [$i][0], $elems [$i][1]) return false; ") = = False) {
$error _message. = $elems [$i][2];

}
}

This allows us to complete the form validation each time we just have to populate the validation array $elems.


I hope it works for you, and I'd like to know how you do it.

This allows us to complete the form validation each time we just have to populate the validation array $elems.

I hope it works for you, and I'd like to know how you do it.

This allows us to complete the form validation each time we just have to populate the validation array $elems.

I hope it works for you, and I'd like to know how you do it.



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.