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
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.

Link to comment
Share on other sites

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;
Edited by The Little Guy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.