I need command that will display error if var is not number

I tried few examples but nothing seems to work, here is what I tried, one worked but it messed up my previous code, if its 0 display other msg, now when its 0 it displays like its string idk why so I removed it

if ($var !== int);

if ($var (!is_number));

if((int)(string)$var == $var);

Recommended Answers

if(!ctype_digit($var)){ echo "NOT DIGIT"; }
Jump to Post

Use elseif instead if in lines 5 and 6 of my code

Jump to Post

Just a word of warning - many "number functions" return unexpected results - so we need to know the full range and type/format of data that you will be testing.

ctype_digit will fail with a number of cases:

Jump to Post

Perhaps more convenient to use the selector instead of a text field

<select name="bodovi">
    <script type="text/javascript">
    for(var i=1; i<=50; i++){
        document.write('<option value="'+i+'">'+i+'</option>');
        }
    </script>
</select>
Jump to Post

You could check the data with a preg_match...

/^\d+$/

I think that will return a positive for 0-100 regardless of whether you have a string or an integer datatype.
You could have an optional period and more digits for floats.

//edit - scrap that - I …

Jump to Post

All 23 Replies

I think you want:

if (is_numeric($var)) {
if(!ctype_digit($var)){ echo "NOT DIGIT"; }

that works but it calls my other code also

elseif ($bodovi == 0) {
echo "Cista 0"; }

so I get msg

Cista 0NOT DIGIT

$errors = array();

// and then test for all errors
if(!ctype_digit($var)){ $errors[] = "NOT DIGIT!"; }
if($var==0){ $errors[] = "IS SET NULL!"; }
if($var==""){ $errors[] = "IS SET EMPTY VALUE!"; }
//........

if(empty($errors)){
    // statement for good user selection
    }
else {
    foreach($errors as $line){
        echo $line."<br/>";
        }
    }

I inserted that code, there is no error but I get when I type text in field

Cista 0NOT DIGIT!
IS SET NULL!

and I need to echo just one instead of 3

Use elseif instead if in lines 5 and 6 of my code

I did, now I get for

 if(!ctype_digit($bodovi)){ $errors[] = "NOT DIGIT!"; }

echo that is null even tho its text

Member Avatar

Just a word of warning - many "number functions" return unexpected results - so we need to know the full range and type/format of data that you will be testing.

ctype_digit will fail with a number of cases: http://www.php.net/manual/en/function.ctype-digit.php#95117

is_int will fail if integer has been cast as a string or if a string contains just digits:

http://stackoverflow.com/questions/236406/is-there-a-difference-between-is-int-and-ctype-digit

There may be a solution here:

http://www.php.net/manual/en/function.ctype-digit.php#108712

However, I think it needs support for strings containing a negative integer.

still showing like its 0 even tho its text, here is code

I get msg Cista 0

I made all other msgs works fine, every msg was simple like this

 if ($bodovi < 0){
    echo "Bodovi ne mogu biti negativni"; }

now I need same thing just to display msg Please type number instead of text if text is typed

function is_digit($bodovi) {
        if(is_int($bodovi)) {
            return true;
        } elseif(is_string($bodovi)) {
            return ctype_digit($bodovi);
        } else {
            // booleans, floats and others
            return false;
        }
    }


if ($bodovi < 0){
echo "Bodovi ne mogu biti negativni"; }


elseif ($bodovi == 0) {
echo "Cista 0"; }

elseif ($bodovi > 50 && $bodovi < 100) {
echo "Uspesno ste polozili ispit"; }

elseif ($bodovi == 100) {
echo "Cestitam vi ste istinski poznavalac php-a"; }

Perhaps more convenient to use the selector instead of a text field

<select name="bodovi">
    <script type="text/javascript">
    for(var i=1; i<=50; i++){
        document.write('<option value="'+i+'">'+i+'</option>');
        }
    </script>
</select>

It has to be php, its simple asigment

if its 0 = echo clear 0

if its 50 = exam passed

if its 100 = best result

if its text = input number not text

and only last one giving me hard time

Member Avatar

You could check the data with a preg_match...

/^\d+$/

I think that will return a positive for 0-100 regardless of whether you have a string or an integer datatype.
You could have an optional period and more digits for floats.

//edit - scrap that - I don't know what I was thinking! You'd gets positives for the most ridiculous combinations :)

so in the end there is no solution for php to check if input is string and do something?

could I try to check if var is in range from 48 to 57 in ASCII and if its not then show error?

Member Avatar

This pattern should work for 0-100:

$p = "/^(?:100|\d{1,2})$/";

I need from 0 to 100 anyway :D let me try this

how can I implement this in if statement or elseif

if ($bodovi = "/^(?:100|\d{1,2})$/");

echo "Please input number";

Member Avatar
$pattern = "/^(?:100|\d{1,2})$/";

if(preg_match($pattern,$bodovi))
{
    //do something
}else{
    //please input number
}

The beauty of regex is that you can use it with javascript as well - but there are some limitations. So, you could do client-side validation on the integer field before the form was sent.

I need it if doesnt match numbers, to display error

with your code I get same echo as I get when input is empty or its 0 and text

and I need for text to be one echo and for 0 another one

Member Avatar

OK. In which case you need the '==='

This matches values AND datatype

$data = "0";
if($data === 0) ... //false
if($data == 0) ...//true

where to insert === here

 if(preg_match($pattern,$bodovi))
    {
  echo "unesite broj";
    }
$pattern = "/^(?:100|\d{1,2})$/";
if(preg_match($pattern,$bodovi))
{
//do something
}else{
//please input number
}

this works, but I need vice versa, with this code it displays error if I type number, I need error if I doesnt type number,

I guess this preg_match should be not match :D

ok I did it with !preg_match

but I have other problems now, typing text calls other echo's too, and typing negative number also calls preg match function so its like this,

if I type text it will show msg "Cista 0unesitebroj"

and if I type -23 it will show msg "Bodovi ne mogu biti negativniunesitebroj"

I highlighted those echos that should not be shown

if ($bodovi < 0){
echo "Bodovi ne mogu biti negativni"; }


if ($bodovi == 0) {
echo "Cista 0"; }

if ($bodovi > 50 && $bodovi < 100) {
echo "Uspesno ste polozili ispit"; }

if ($bodovi == 100) {
echo "Cestitam vi ste istinski poznavalac php-a"; }


$pattern = "/^(?:100|\d{1,2})$/";
if(!preg_match($pattern,$bodovi))
{

echo "unesite broj";

}

P.S I fixed text problem, now I only have left negative number conflicting with preg match, so if I type -23 I get msg both for if < 0 and for preg match

Is there option to include text only in preg match? or 0-100 only? it would be much easier for my code

I need solution in 2,3 days max, if possible tomorrow, so if anyone knows how to do this help

thanks in advance

Be a part of the DaniWeb community

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