Jump to content

Validating Text Boxes


dark_destroyer

Recommended Posts

Hi All,

 

I have a straightforward page that has about 30 text boxes in it.

But... theres always a but...

 

I want to be able to validate the text before update the record in MySQL.

For example the IMEI field should have a minimum of 15 chars and no spaces..

 

Ive used:

if (strpos($myString, " ") !== false)

to check for blank spaces on a single field before, but i know what to check multiple fields.

 

What is the best way to do this, should I:

 

1. Use a lot of elseif statements

 

2. Add all the values of the text boxes to a variable and insert/update them one by one using an if & elseif statement

 

Can anyone suggest the "proper" way to do this?

 

Link to comment
https://forums.phpfreaks.com/topic/277447-validating-text-boxes/
Share on other sites

you would create functions or call built in php functions for each different type of validation. since you may have different requirements for each form field, i would make an array that lists the validation function names that you want to call in the order that you want to call them. then for each field, get the list of validation functions out of the array and dynamically call them using variable functions - http://php.net/manual/en/functions.variable-functions.php

 

your code would loop over the expected form fields, getting the field name, then access the list of validation function names from the array and dynamically call each validation function and test/store the result. you could also expand upon this so that the array holds the corresponding error message and when a validation step fails, get and store the corresponding error message that you want to display.

It might be easiest to have a configuration, that tells PHP how the textbox should be formatted (min/max length, numbers only, alpha only, etc.).

 

It would/could look something like this:

$config = [
    "myField" => [
        "max" => 15,
        "min" => 6
    ]
];  // Array is php 5.4 syntax
function validate_field($field_name, $value, &$error){
    global $config; // don't use global, I am for this example though
    $settings = $config[$field_name];
    if(isset($settings["max"]) && $settings["max"] > strlen($value)){
        $error = "Too Long";
        return false;
    }
    if(isset($settings["min"]) && $settings["min"] < strlen($value)){
        $error = "Too Short";
        return false;
    }
    $error = "";
    return true;
}

$errors = [];
foreach($_POST as $name => $value){
    if(!validate_field($name, $value, $error)){
        // The field wasn't valid
        $errors[] = $error;
    }
}

if(count($errors) > 0){
    // The form has errors
    echo implode(",", $errors);
    // Show form again
    exit;
}
header("Location: to success");
exit;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.