Page 1 of 1
My function isn't returning the value I'm expecting.
Posted: Thu Aug 28, 2003 3:36 pm
by Crashin
Hey all. I've got a form in which EU's can enter the version number of our app that they are currently using. The version numbers are in the form:
1.2.3
On my subsequent processing page I want to make sure the EU entered a numeric-only version number, with the exception of the periods. So, I've got the following function in an include file:
Code: Select all
<?php
function validate_version($version) {
$versArray = split('[.]', $version);
$num = count($versArray);
for ($i=0; $i<$num; $i++) {
return is_numeric($versArray[$i]);
}
}
?>
I'm calling the function like so:
Code: Select all
<?php
if (!validate_version($_POST['currentVersion'])) {
$error_code = "invalid version number";
}
?>
Yet, the processing page is letting anything through. If I enter
1.2.3
it allows it (which is correct). If I enter
1.2.b
it also allows it (which is incorrect). What am I doing wrong?
Posted: Thu Aug 28, 2003 4:34 pm
by qads
Code: Select all
<?php
function validate_version($version)
{
$split = explode(".", $version);
for($x = 0; $x<=count($split); $x++)
{
if(!empty($split[$x]))
{
if(is_numeric($split[$x]))
{
$msg = 1;
}
else
{
$msg = "Not A Number";
}
}
}
return $msg;
}
?>
a quick way of doing it, didn't check, might be a error somewhere

Posted: Thu Aug 28, 2003 7:11 pm
by McGruff
There is a logic error in the function. It returns a value (and exits the function) on the first loop and so it will test true for any string which begins with an integer regardless of the other values.
The revised version posted by qads is getting there but also has a logic error - kind of the reverse of the first fn. $msg is reset on each loop and so, in effect, it only tests the final character in the string.
I didn't test this but should work:
Code: Select all
<?php
// returns true if $version validates successfully, false if not
function validate_version($version)
{
$versArray = split('[.]', $version);
$num = count($versArray);
$errors = TRUE;
for ($i=0; $i<$num; $i++)
{
if(!is_numeric($versArray[$i]))
{
$errors = FALSE;
}
}
return $errors;
}
?>
Posted: Fri Aug 29, 2003 7:59 am
by qads
um..mine works fine McGruff

, just tested it.
Code: Select all
<?php
function validate_version($version)
{
$split = explode(".", $version);
for($x = 0; $x<=count($split); $x++)
{
if(!empty($split[$x]))
{
if(is_numeric($split[$x]))
{
$msg = 1;
}
else
{
$msg = "Not A Number";
}
}
}
return $msg;
}
echo validate_version($_POST[num]);
?>
<form action="" method="post">
<input type="text" name="num">
<input type="submit" value="Check Value" name="x">
</form>
Posted: Fri Aug 29, 2003 9:54 am
by liljester
Code: Select all
<?php
function validate_verstion($version){
if( is_numeric(str_replace(".", "", $version)) && strchr($version, ".") ){
return true;
}
else { return false; }
}
?>
Posted: Fri Aug 29, 2003 11:57 am
by Crashin
Thanks for the replies, everyone. Sorry I didn't get back to the post yesterday. Went to the Jack Johnson/Ben Harper show at Red Rocks...it was AMAZING!

I'm running on only a couple hours of sleep right now, but it's all good! (I think I'm still feeling the "effects" of the show...

)
Hey qads...your function works partly. If the last character in the entry is not numeric, the function will return the "error" (i.e. "Not A Number"), which is correct. Likewise, if the string is correct the function returns the "true" value (i.e. 1). However, if you enter something like
a.1.2
the function will still return "1", even though there is an "a" in the string.
McGruff's function is working correctly.

I haven't had time to try/test liljester's, but it looks like it will also do the trick.
Thanks again for your help everyone!
Posted: Fri Aug 29, 2003 4:06 pm
by qads

i only tested it with your example before so no wonder its not working

Posted: Fri Aug 29, 2003 5:57 pm
by McGruff
Just one minor point about the fn I posted. This assumes the result is TRUE and only changes the TRUE state if it finds an error. That doesn't really matter in a simple fn like this - the for loop will always run - but in a more complex system (ie a whole bunch of fns or objects all interacting with each other) it's probably safer to work the other way around, ie assume the result is FALSE just in case something doesn't run for some reason.