Jump to content

Number validation not returning the correct results


nvee

Recommended Posts

I have a simple function which checks if a variables passed into the function is numeric and if it is the lenght it should be. Somehow it keeps on returning the error, even if the number is correct.

 

code in html ...

INTVALIDATE($tel,10);

 

function code ...

function INTVALIDATE($var,$lenght) {
$value = strlen($var);
if($var != $value AND !is_numeric($var)) {
	$intresult = "<li>Some of your numbers does not match the correct lenght or is not numeric, please correct this!</li>";
}
return $intresult;
} 

Your function isn't making too much sense to me (hung over), what are the specifications of your function?

You pass a 'length', but then not use it anywhere!

The 'if' would probably be an 'OR', not 'AND'.

If it's for telephone numbers then i'd expect to receive spaces, ext's, prefixes, codes, etc... Therefore i'd suggest looking at PCRE (e.g. regex), see the manual ([url=http://uk2.php.net/manual/en/function.preg-match.php]preg-match

. Also I did a quick google search for 'pcre telephone' and the first result was:

 

http://benramsey.com/tag/pcre/

define("MIN_RANGE", 0); //Minimum integer amount desired...
define("MAX_RANGE", 100); // Maximum integer amount desired...

$options = array( 'options' => array( 'min_range' => MIN_RANGE, 'max_range' => MAX_RANGE ) );
$var     = 50;

if ( filter_var( $var, FILTER_VALIDATE_INT, $options ) === true )
{
   # Var validated as int between 0 and 100
}
else
{
   # Var didn't validate...
}

 

 

filter_var

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.