Is there a cleaner way to do this?

im using this to secure form inputs


if (!preg_match("/^[a-zA-Z0-9_]{1,15}$/", $_POST['size'])) {die("error, not valid"); exit();}
if (!preg_match("/^[a-zA-Z0-9_]{1,15}$/", $_POST['quantity'])) {die("error, not valid"); exit();}
if (!preg_match("/^[a-zA-Z0-9_]{1,15}$/", $_POST['item'])) {die("error, not valid"); exit();}

im guessing theres got to be a cleaner way to do that, especially when i have more variables… i tried this, but it didnt work


if (!preg_match("/^[a-zA-Z0-9_]{1,15}$/", $_POST)) {die("error, not valid"); exit();}

thanks

You could register the variables before the if statements so that you can use $size = $_POST[‘size’] and then call $size (etc)?

You could use either

$fields = array('size', 'quantity', 'item');

foreach($fields as $field) {
    if (!preg_match("/^[a-zA-Z0-9_]{1,15}$/", $_POST[$field])) {
        die("error, not valid");
    }
}

[B]OR

[/B]

foreach($_POST as $field) {
    if (!preg_match("/^[a-zA-Z0-9_]{1,15}$/", $field)) {
        die("error, not valid");
    }
}

thanks, second one worked great

No problem