Re: Frustation with data validation

From: Date: Sun, 23 Feb 2003 21:59:11 +0000
Subject: Re: Frustation with data validation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-136759@lists.php.net to get a copy of this message
any $_POST data is actually string data. Use these functions from a class of mine I plan on making public when I have it thoroughly tested. function isString( $var_name, $var ) { /*Nothing else I can do in this case.. I don't think any other data type can have a length of zero. If I jsut check for is_string and empty string will not register as a string.*/ if ( strlen( $var ) == 0 ) { return; } if ( !is_string( $var ) ) { $this->{$this->log_func} ( "$var_name, '$var', does not contain valid string data." ); } } function strIsInt( $str ) { $this->isString( 'str', $str ); if ( strlen( $str ) == 0 ) { return FALSE; } for ( $i = 0; $i < strlen($str); $i++ ) { if ( !ctype_digit ( $str[$i] ) && $str[$i] != '-' ) { return FALSE; } } return TRUE; } On February 23, 2003 05:02 pm, Robert E. Harvey, M.D. wrote: > First, thanks to all who offered suggestions with my "simple ereg > question". I have been unable to get anything to work "properly" in > spite of many good suggestions. The problem I want to solve is to > ensure that input fields have either an integer number or are blank > before the program proceeds. I am approaching the problem thusly: > > for ($i=1;$i<=3;$i++) > { > echo $_POST["grass_$i"]; // Pollen name > echo $_POST["gamt_$i"]; // Pollen amount > if (is_int($_POST["gamt_$i"])) > { > continue; > } > else > { > die("Non-numeric data entered in grass entry field(s)."); > } > } > > The two echo statements accurately return what data is in the two input > fields. The program dies when there is integer data returned in the > echo $POST["gamt_$i"] statement. > > If I negate the expression in the if statement like so : if > (!is_int($_POST["gamt_$i"])) > the program runs. Isn't that a bit weird? > > It gets weirder. If I enter an alpha character with if > (!is_int($_POST["gamt_$i"])), the program still runs but treats the > alpha character as a 0. > > How should I construct that if statement so that the program will only > run with integer data or no data entered into the input fields? > > TIA > > Bob Harvey

« previous php.general (#136759) next »