Jump to content

[php] How to check if the input variable is integer?


michalchojno

Recommended Posts

I have a variable submitted throu html form, input tag. I'd like to check if this variable is integer. I used is_numeric() function, but I get an error message when nothing is input in the window.

 

Will is_int() function work better?

 

There is also ereg() function, but I can't figure out how it really works? What's this: [0-9]{4})-([0-9]{1,2} stuff??? What's the right syntax and logic?

Example:

ereg("^[1-9][0-9]*$", $_POST['variable'])

 

How can I make it that when nothing is input, I won't get an error and it will be the same as 0 value?

Link to comment
Share on other sites

Not really what I meant. Here is the code I have:

if (!ereg("^[1-9][0-9]*$",($_POST[$val]))) {

echo "Error.";

die();

}

When the input field is empty, I get error. How to make it 0 if empty?

 

EDIT: I changed it to the following and it seems to work.

if (!ereg("^[1-9][0-9]*$",($_POST[$val])) && $_POST[$val] != 0) {

echo "Error.";

die();

}

Link to comment
Share on other sites

http://us2.php.net/manual/en/function.ctype-digit.php might help you in this case.  Much easier than regex.

 

<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
    if (ctype_digit($testcase)) {
        echo "The string $testcase consists of all digits.\n";
    } else {
        echo "The string $testcase does not consist of all digits.\n";
    }
}
?>

The above example will output:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.

Link to comment
Share on other sites

Still could someone please explain how ereg() works? The syntax seems weired. Example: ereg("^[1-9][0-9]*$",($_POST[$val])). Why "^" at the start? Why [1-9[0-9]? Why "*$"?

 

The regular expressions says:

 

Start of string (^) followed by a character in the range 1-9 ([1-9]) followed by a character in the range 0-9 zero or more times ([0-9]*) followed by end of string ($).

 

Google: "regex"

And see: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

 

By the way, preg is faster than ereg.

Link to comment
Share on other sites

^[1-9][0-9]*$

 

That is a regular expression.  The "^" represents the beginning of the string and "$" represents the end.  These are necessary because without them, the above expression would also match strings like: "blah blag 83983 roflcopter".

 

[1-9] matches a number 1-9 obviously.

 

[0-9]* matches any number of numbers 0-9.

 

So it looks like this expression is meant to match any number equal to or greater than 1.

Link to comment
Share on other sites

Geeez, frustrating, It doesn't work... :(

 

If the field is empty (a person didn't put anything in), I'd like to allow it and treat it as 0. Instead I keep getting "Error".

 

Here is what I typed, which doesn't seem to fix it.

if (empty($val)) {$val = 0;}

if (!ctype_digit($val)) {

echo "Error";

die();

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.