Jump to content

[SOLVED] convert script snippet to function


AV1611

Recommended Posts

this simple script verifies that a number is VALID based on the following

 

number must be 4 NUMBERS decimal 3 NUMBER (4455.333)

first number must be > 100 (0100.123 is valid but 0099.123 is not valid)

number can contain no alpha characters (a123.123 is is not valid)

 

QUESTION:

 

can someone help me convert this to a function? I simply want to function to either output "VALID" or "INVALID" or TRUE/FALSE

 

$t1="4455.003";
// begin test of input number $t1
$TL = strlen($t1);
// Verify the number is 8 Characters Long
if($TL != {
echo '<br/>'.$t1.' Is Not A Valid Part Number Series...';
die();
}
$split=explode('.',$t1);
// Verify the number is divisible by a "."
if(!isset($split[0]) || !isset($split[1]) || isset($split[2])){
echo '<br/>'.$t1.' Is Not A Valid Part Number Series...';
die();
}
$L1=$split[0];
$L2=$split[1];
$LL1 = strlen($L1);
$LL2 = strlen($L2);
// Verify the number was divided in to ONLY two parts,
// not three and that the parts only contain numbers and
// the first number is 4 digits and the second number is 3 digits and
// the first number is at least 0100
if($LL1 != 4 || $LL2 !=3 || !is_numeric($L1) || !is_numeric($L2) || $L1 <= 99) {
echo '<br/>'.$t1.' Is Not A Valid Part Number Series...';
die();
}
else{
echo $L1.".".$L2." is a VALID Number";
echo "<br/>";
}

<?php
function verifyNumber($t1) {
// begin test of input number $t1
$TL = strlen($t1);
// Verify the number is 8 Characters Long
if($TL != {
echo '<br/>'.$t1.' Is Not A Valid Part Number Series...';
die();
}
$split=explode('.',$t1);
// Verify the number is divisible by a "."
if(!isset($split[0]) || !isset($split[1]) || isset($split[2])){
echo '<br/>'.$t1.' Is Not A Valid Part Number Series...';
die();
}
$L1=$split[0];
$L2=$split[1];
$LL1 = strlen($L1);
$LL2 = strlen($L2);
// Verify the number was divided in to ONLY two parts,
// not three and that the parts only contain numbers and
// the first number is 4 digits and the second number is 3 digits and
// the first number is at least 0100
if($LL1 != 4 || $LL2 !=3 || !is_numeric($L1) || !is_numeric($L2) || $L1 <= 99) {
return false;
}
else{
return true;
}
}

if (verifyNumber(4455.003)) {
     echo 'Valid';
}else {
     echo 'invalid';
}
?>

<?php

function verify($t1) {

// begin test of input number $t1
$TL = strlen($t1);
// Verify the number is 8 Characters Long
if($TL != {
return false;
}
$split=explode('.',$t1);
// Verify the number is divisible by a "."
if(!isset($split[0]) || !isset($split[1]) || isset($split[2])){
return false;
}
$L1=$split[0];
$L2=$split[1];
$LL1 = strlen($L1);
$LL2 = strlen($L2);
// Verify the number was divided in to ONLY two parts,
// not three and that the parts only contain numbers and
// the first number is 4 digits and the second number is 3 digits and
// the first number is at least 0100
if($LL1 != 4 || $LL2 !=3 || !is_numeric($L1) || !is_numeric($L2) || $L1 <= 99) {
return false;
}
else{
return true;
}
}

if(verify(4455.003)) {
echo "true";
} else {
echo "false";
}

?>

 

--- EDIT ---

 

Grr... beaten :P

That really is quite easy... I need to start writing functions instead of doing everything long each time :P

 

Thanks for all the help.  That may be the clearest example of how to create a function from simple script.  It will go a long way in my learning...

 

 

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.