float Number validation check for codeigniter - php

Here I am entering tax field in % but when i enter values like 2.5,0.5 other than integer it is generating error.
Here is my code for Validation,any idea for entering float numbers
function _set_rules()
{
$this->form_validation->set_rules('pst','PST','trim|required|is_natural|numeric|
max_length[4]|callback_max_pst');
$this->form_validation->set_rules('gst','GST','trim|required|is_natural|numeric|
max_length[4]|callback_max_gst');
}
function max_pst()
{
if($this->input->post('pst')>100)
{
$this->form_validation->set_message('max_pst',' %s Value Should be less than or equals to 100');
return FALSE;
}
return TRUE;
}
function max_gst()
{
if($this->input->post('gst')>100)
{
$this->form_validation->set_message('max_gst',' %s Value Should be less than or equals to 100');
return FALSE;
}
return TRUE;
}
</code>

remove the is_natural from the validation rules and replace it with greater_than[0] and less_than[100]
function _set_rules()
{
$this->form_validation>set_rules('pst','PST','trim|required|
greater_than[0]|less_than[100]|max_length[4]|callback_max_pst');
$this->form_validation->set_rules('gst','GST','trim|required|
greater_than[0]|less_than[100]|max_length[4]|callback_max_gst');
}
greater_than[0] will apply numeric

From the codeigniter documentation:
is_natural Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. source
Clearly, values like 2.5,0.5 are not natural numbers so they will fail validation. You can use a callback and return the value after parsing the value with floatval() PHP function.
Hope it helps!

You can try this :
function _set_rules()
{
$this->form_validation>set_rules('pst','PST','trim|required|
numeric|max_length[4]|callback_max_pst');
$this->form_validation->set_rules('gst','GST','trim|required|
numeric|max_length[4]|callback_max_gst');
}
function max_pst($value) {
$var = explode(".", $value);
if (strpbrk($value, '-') && strlen($value) > 1) {
$this->form_validation->set_message('max_pst', '%s accepts only
positive values');
return false;
}
if ($var[1] > 99) {
$this->form_validation->set_message('max_pst', 'Enter value in
proper format');
return false;
} else {
return true;
}
}
Hope this code will help you.... :)

Related

Set function value based on variable

I need to assign the variable Freight_class to the value: Bigheavy, Largebox or Littlebox. For that I have to use the value of Freight. If the value of Freight is greater than 225, Freight_class must be set to Bigheavy. If Freight is greater than 99, Freight_class must be equal to Largebox. Under 99, Freight_class must be Littlebox.
Say Freight=40, then Freight_class should be Littlebox. But I can not get it to work. What do I do wrong
<?php
$Freight=40;
function Freight_class($Freight) {
if ($Freight > '225') {
return ('Bigheavy');
} elseif ($Freight > '99') {
return ('Largebox');
} else {
return ('Littlebox');
}
}
echo $Freight_class;
You never actually call your function. And $Freight_class is not defined anywhere.
<?php
$Freight=40;
function Freight_class($Freight) {
if ($Freight > 225) {
return ('Bigheavy');
} elseif ($Freight > 99) {
return ('Largebox');
} else {
return ('Littlebox');
}
}
$Freight_class = Freight_class($Freight); // assign the result of your function to $Freight_class
echo $Freight_class;
<?php
$Freight=40;
function Freight_class($Freight) {
if ($Freight > 225) {
return ('Bigheavy');
} elseif ($Freight > 99) {
return ('Largebox');
} else {
return ('Littlebox');
}
}
echo Freight_class($Freight);
This way it would work. You have some logical errors in your code. First, it seems like youre confused with the terms class and function it looks like you tought to create a class with your function Freight_class but later your using it like a normal variable.
Next point is that you compare strings with integers which could lead to strange bugs.
Working code with comments:
<?php
$Freight=40;
// the fact that the parameter has the same hame as the variable
// does not mean that it is using the variable. The new $Freight
// is block scoped and prevents access of the outer $Freight
function Freight_class($Freight) {
// while comparing number you should use numbers and not a string
// containing a number
if ($Freight > 225) {
return 'Bigheavy';
} elseif ($Freight > 99) {
return 'Largebox';
}
return ('Littlebox');
}
// since Freight_class is a function and the inner $Freight differs
// from the outer one, you have to call Freight_class with the outer
// $Freight as parameter
echo Freight_class($Freight);

PHP Check if multiple values in array are all positive or all negative or partly

I have an array with 2 possible values negative and positive. If all the values are positive my function has to return positive.
If all values are negative my function has to return negative. If the values are a mix then my function has to return partly.
My code always returns partly, unfortunately, I don't know why.
const RESULT_OF_SEARCH_POSITIVE = "positive";
const RESULT_OF_SEARCH_NEGATIVE = "negative";
const RESULT_OF_SEARCH_PARTLY = "partly";
...
private function calculateResultOfSearch(array $imagesResultArray)
{
if (array_unique($imagesResultArray) === self::RESULT_OF_SEARCH_POSITIVE) {
return self::RESULT_OF_SEARCH_POSITIVE;
} elseif(array_unique($imagesResultArray) === self::RESULT_OF_SEARCH_NEGATIVE)
{
return self::RESULT_OF_SEARCH_NEGATIVE;
} else {
return self::RESULT_OF_SEARCH_PARTLY;
}
}
As we know the count() function always returns the count of the array. So it goes to the else case in every match of the condition.
You should try something like this:
class Demo{
const RESULT_OF_SEARCH_POSITIVE = "positive";
const RESULT_OF_SEARCH_NEGATIVE = "negative";
const RESULT_OF_SEARCH_PARTLY = "partly";
function calculateResultOfSearch(array $imagesResultArray)
{
if (count(array_count_values($imagesResultArray)) == 1 && $imagesResultArray[0] === self::RESULT_OF_SEARCH_POSITIVE) {
return current($imagesResultArray);
} elseif(count(array_count_values($imagesResultArray)) == 1 && $imagesResultArray[0]== self::RESULT_OF_SEARCH_NEGATIVE) {
return self::RESULT_OF_SEARCH_NEGATIVE;
} else {
return self::RESULT_OF_SEARCH_PARTLY;
}
}
}
$demo = new Demo();
print_r($demo->calculateResultOfSearch(["positive","positive"]));
array_count_values() returns an array using the values of the array as keys and their frequency in the array as values.
Here is a simple way to check the values of an array containing the same value using array_count_values function and count if all keys are the same this should equal.
Reference
A much simplified version of the code which if array_unique just has 1 value, then return it (also I only call it once rather than repeatedly calling it which is very inefficient)...
private function calculateResultOfSearch(array $imagesResultArray)
{
$unique = array_unique($imagesResultArray);
return (count($unique) == 1 ) ? $unique[0]
: self::RESULT_OF_SEARCH_PARTLY;
}
Edit:
I am unfortuantly back after realising I've wasted 20% of the lines I wrote :( If all the items are the same, I can just return the first item of the array passed in so I don't need to store the result of array_unique() at all :-/
private function calculateResultOfSearch(array $imagesResultArray)
{
return ( count(array_unique($imagesResultArray)) == 1 ) ?
$imagesResultArray[0]: RESULT_OF_SEARCH_PARTLY;
}
Try this code :
const RESULT_OF_SEARCH_POSITIVE = "positive";
const RESULT_OF_SEARCH_NEGATIVE = "negative";
const RESULT_OF_SEARCH_PARTLY = "partly";
...
private function calculateResultOfSearch(array $imagesResultArray)
{
if (!in_array(RESULT_OF_SEARCH_NEGATIVE)) {
return self::RESULT_OF_SEARCH_POSITIVE;
} elseif(!in_array(RESULT_OF_SEARCH_POSITIVE)) {
return self::RESULT_OF_SEARCH_NEGATIVE;
} else {
return self::RESULT_OF_SEARCH_PARTLY;
}
}
You're comparing (count(array_unique($imagesResultArray)) which return an int with self::RESULT_OF_SEARCH_POSITIVE or self::RESULT_OF_SEARCH_NEGATIVE which return a string equals to either "positive" or "negative" so it's always false.
You need to change your conditions.
EDIT after OP's edit
PHP: array_unique
Takes an input array and returns a new array without duplicate values.
In your case you might want to check if the resulting array only has one element equals to "positive" or "negative".
$newArray = array_unique($imagesResultArray);
if (count($newArray) == 1 && $newArray[0] === self::RESULT_OF_SEARCH_POSITIVE) {
return self::RESULT_OF_SEARCH_POSITIVE;
} elseif(count($newArray) == 1 && $newArray[0] === self::RESULT_OF_SEARCH_NEGATIVE) {
return self::RESULT_OF_SEARCH_NEGATIVE;
} else {
return self::RESULT_OF_SEARCH_PARTLY;
}
With your guys answers I came to this "clean" solution.
private function calculateResultOfSearch(array $imagesResultArray)
{
$results = array_unique($imagesResultArray);
if(count($results) == 1 && $results[0] === self::RESULT_OF_SEARCH_NEGATIVE) {
return self::RESULT_OF_SEARCH_NEGATIVE;
}
if(count($results) == 1 && $results[0] === self::RESULT_OF_SEARCH_POSITIVE) {
return self::RESULT_OF_SEARCH_POSITIVE;
}
return self::RESULT_OF_SEARCH_PARTLY;
}

use the functions without its parameters

I am using 2 regex functions here and I wanna make another function which returns false when the 2 regex are both false and if not, then true.
The problem here is when I wanna use the 2 regex functions in the third one, I have to give them parameters, which is not necessary I think, because the third function will only return a simple true or false. I get an undefined variable whenever I give parameters to the 2 regex functions in the 3rd one.
I tried using global variables which works but since its a bad practice I am looking for a better solution.
Code:
function regex1($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function regex2($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function checkBoth()
{
if (regex1($input) === false || regex2($input) === false)
{
return false;
}
else
{
return true;
}
}
EDIT:
The checkBoth function I am using in my other file like this together with the other 2 regex functions:
if (!regex1($input))
{
// show error at the same time
}
if (!regex2($input))
{
// show error at the same time
}
if(checkBoth())
{
// success
}
function regex2($input,$secondVar=false)
{....
Later in code in place where you need just add:
if($secondVar !== false){
// do whatever...
}
If you can't user "false" you can just empty string '' or any other value that will not appear there.

PHP : Function - Return 2 variables (Boolean + $variable)

Issue Resolved : Here is the solution :
function testCoreq()
{
$coreqTest = makeCoreq();
if(empty($coreqTest))
{
return array(true);
break;
}
else
{
foreach ($coreqTest as $ctest)
{
if($ctest['value'] == "true")
{
return array(true);
break;
}
else
{
return array(false,$ctest['coreqID']);
}
}
}
}
if(testCoreq()[0])
{
//do something
}
else
{
return testCoreq()[1]
}
I'm doing a school project and hit kind of a bump.
I created a function and i want it to either return "true" (boolean) or "false" (boolean) + a variable.
I searched the net quite a bit but wan't able to find a simple way to do this .
Is there any way to this this ? //Thanks
The function is working properly but when it is returning the variable - it is also assuming that the function is returning "true" when i want it to return false + the value like :
else
{
return $ctest['coreqID'];
return false;
}
Here is the code :
function testCoreq()
{
$coreqTest = makeCoreq();
if(empty($coreqTest))
{
return true;
break;
}
else
{
foreach ($coreqTest as $ctest)
{
if($ctest['value'] == "true")
{
return true;
break;
}
else
{
return $ctest['coreqID'];
}
}
}
}
I am using it like this:
if (testCoreq())
{
// do something
}
else
{
// return the variable
}
but even if the first statement is false , then it is returning the variable - it is assuming the function is true.
You can try to return -1 on true and other for false to decrease amount of returning values. Next option is to return array of values. The other option would be to pass reference variable in the function.

Test only evaluating first element of array

I loop through the values of a form, to check that each field has 4 digits. My problem is currently it validates true or false only on the match for the first field $card1...
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
else
{
return true;
}
}
You're returning (by using return ...) something in the first iteration every time (boolean condition with an else).
You need to put the return true outside the loop statement:
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++) {
if (! preg_match ($regex,$cards[$i])) {
return false;
}
}
return true;
}
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
return true;
}

Categories