1

Little confused about how to code this. User inputs numbers, then the validation checks to make sure it is numeric and positive. The user can also leave this field blank.

This is what i have so far, it just checks to see that something was inserted.

$error_blue = check_blue($phone);
if($error_blue !=''){
    print "<p>Blue: $error_blue";
    }

Is where the item is validated at the top of the page.

function check_blue($blue){
if(! is_numeric($blue)){
    return'Please Enter a valid number for Blue.';
}}

Is where the function is. Any help on what to do here would be much appriciated.

4 Answers 4

2

Something like this?

function check_blue($blue) {
    if (is_numeric($blue) && $blue > -1) {
        return 1;
    }
    else {
        return 0;
    }
}

if(!check_blue($phone)) {
    return'Please Enter a valid number for Blue.';
}

See the demo

Sign up to request clarification or add additional context in comments.

1 Comment

@user2168066 I don't follow.
0

Assuming it's an integer you expect,

if ((int)$blue==abs($blue) || empty($blue)) $error = false;
else $error = true;

Then you use $error to decide what to do.

Comments

0

Everyone here was using if else. It is not that this is wrong, but just to show you another idea, which might be useful for other type of validation, take a look at validate filters : http://www.php.net/manual/en/filter.filters.validate.php For this purpose you might use FILTER_VALIDATE_INT

Comments

0

You might want to use something simple as

function check_blue($phone) {
  return (empty($phone) || (is_numeric($phone) && $phone >= 0));
}

First check if the field is blank, nothing to do if it is. If it is not blank it will be checked for numericality and to being greater or equal zero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.