Jump to content

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...

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.