Hi

I am trying to add a small piece of code to a script to validate two user inputs.

  1. Whether a minimum bid is less that 20 - if so show an error; and
  2. Check that the maximum bid is higher than the minimum bid - if not show an error

The existing code checking other inputs is:

                    if(strlen($_POST['project_name']) <= 0)
                    {
                        $errors[]['message'] = $lang['PROJECTTITLEMISSING'];
                    }

                    if(!isset($_POST['jobtype']))
                    {
                        $errors[]['message'] = $lang['PICK1JOBTYPE'];
                    }

                    if(strlen($_POST['project_description']) <= 0)
                    {
                        $errors[]['message'] = $lang['PROJECTDESCRIPTIONMISSING'];
                    }

I have tried

                    if(!isset($_POST['budget_min']) < 20)
                    {
                        $errors[]['message'] = $lang['ERRORMINBUDGET'];
                    }

to check whether the budget_min is less than 20 and if so show an error. This shows the error but regardless of the value entered. How do I alter this to check the numeric value?

Thereafter, I need to check that budget_max is greater than or equal to budget_min and would be grateful if someone could point me in the right direction.

Many thanks
Mark

Recommended Answers

All 5 Replies

Hey,

Look at this:

<?php

 $budget_min = 10;
 $budget_max = 10;

 if(!isset($budget_min) && strlen($budget_min) < 20)
 {
    // display the errror   

 }

 // Check if the value entered is numeric
 if(!is_numeric($budget_min))
 {
    // display the error

 }

 if($budget_min >= $budget_max)
 {


 }

?>

Hope this helps :)

Hi

Thank you for this.

I have tried this plus a simpler variation as follows:

if(($budget_min) < 20)
 {
    $errors[]['message'] = $lang['ERRORMINBUDGET'];  

 }


 if($budget_min > $budget_max)
 {
$errors[]['message'] = $lang['ERRORMAXBUDGET']; 

 }

as it is the value of budget_min rather than the length that I an trying to test. However, every time regardless of the value I enter on the input form the I get the error message that $budget_min is < 20 and no message for $budget_max when I set it lower. I have very limited knowledge of all of this but I am assuming that for some reason $budget_min and $budget_max must be zero and not have a value?

Am I explaining this properly?

Thanks
Mark

You might have to cast the variables, by this, I mean specifically define the variable type. Look at this example:

<?php

    $budget_min = (int) 20; // CAST the variable (int)
    $budget_max = (int) 10; // CAST

    if($budget_min <= 20)
    {
        echo 'The budget is too small!';
    }else{
       echo 'The budget is  fine!'; 
    }

    if($budget_min > $budget_max)
    {
        echo 'The budget is higher than the max budget';

    }
    // Output:
    // The budget is too small!
    // The budget is higher than the max budget
?>

So in terms of your example:

$budget_min = (int) $_POST['budget_min']; 

And then you can even be more specific and go:

if(!is_numeric($_POST['budget_min']))
  die("Not a number");

Hope this helps, try clearing up what you want/need?

Thank you.

That worked a treat!

Thanks again

Mark

No prblem :)

Please mark this thread as solved, and, give rep if you feel someone has helped you!

Goodluck with your coding/application

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.