shematic/did not tested example:
<?php
function check( $index , $label, $required , $min , $max )
{
$errors = '';
$value = isset( $_POST[$index] ) ? $_POST[$index] : null ;
if ( $required AND empty( $value ) )
$errors .= $label . ' is empty<br />';
else {
if ( strlen( $value ) > $max )
$errors .= $label . ' is longer then' . $max . "<br />";
if ( strlen( $value ) < $min )
$errors .= $label . ' is shorten then' . $min . "<br />";
}
return $errors;
}
// how to call it:
if ( isset( $_POST["submit"] ) ) {
$errors = '';
// ( $index , $label, $required , $min , $max )
$errors .= check( "un" , "Username" , 1 , 2 , 50 );
$errors .= check( "pass" , "Password" , 1 , 2 , 50 );
$errors .= check( "email" , "Email" , 1 , 2 , 50 );
$errors .= check( "message" , "Message" , 1 , 2 , 50 );
if ( empty( $errors ) ) {
// do something
} else {
print "Check these errors:" . $errors;
}
}
?>