AV1611 Posted May 13, 2007 Share Posted May 13, 2007 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/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/51196-solved-convert-script-snippet-to-function/ Share on other sites More sharing options...
per1os Posted May 13, 2007 Share Posted May 13, 2007 <?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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51196-solved-convert-script-snippet-to-function/#findComment-252093 Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/51196-solved-convert-script-snippet-to-function/#findComment-252094 Share on other sites More sharing options...
AV1611 Posted May 13, 2007 Author Share Posted May 13, 2007 That really is quite easy... I need to start writing functions instead of doing everything long each time 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... Quote Link to comment https://forums.phpfreaks.com/topic/51196-solved-convert-script-snippet-to-function/#findComment-252363 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.