By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,510 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

how to validate $_GET var is a positive integer?

P: n/a
I need to check the $_GET['idnum'] to make certain it is a positive integer.

is_integer($_GET['idnum']) is not working. I think it thinks it's a sting.
So I tried casting it to an int using, is_integer((int)$_GET['idnum']).
Then whatever I threw in the _GET variable passed as an integer, even
strings.

Any ideas or links or functions are much appreciated!
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
*** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):
I need to check the $_GET['idnum'] to make certain it is a positive integer.

is_integer($_GET['idnum']) is not working. I think it thinks it's a sting.
So I tried casting it to an int using, is_integer((int)$_GET['idnum']).
Then whatever I threw in the _GET variable passed as an integer, even
strings.


Manual says:

"To test if a variable is a number or a numeric string (such as form input,
which is always a string), you must use is_numeric()."

In any case, for most apps it's enough with (int)$_GET['idnum']; of course,
using that value in your calculations, not only to validate. The worst
problem you can face is that actual value is "3a" or "3.25" and you get a
3.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #2

P: n/a
right, and thanks. '3a' is exactly what I am trying to test for - I am
posting another main topic to cover this
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message
news:x4***************************@40tude.net...
*** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):
I need to check the $_GET['idnum'] to make certain it is a positive
integer.

is_integer($_GET['idnum']) is not working. I think it thinks it's a
sting.
So I tried casting it to an int using, is_integer((int)$_GET['idnum']).
Then whatever I threw in the _GET variable passed as an integer, even
strings.


Manual says:

"To test if a variable is a number or a numeric string (such as form
input,
which is always a string), you must use is_numeric()."

In any case, for most apps it's enough with (int)$_GET['idnum']; of
course,
using that value in your calculations, not only to validate. The worst
problem you can face is that actual value is "3a" or "3.25" and you get a
3.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la
intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--

Jul 17 '05 #3

P: n/a
"NotGiven" wrote:
right, and thanks. ’3a’ is exactly what I am trying to
test for - I am
posting another main topic to cover this
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in
message
news:x4***************************@40tude.net...
*** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):
I need to check the $_GET[’idnum’] to make certain it is a positive integer.

is_integer($_GET[’idnum’]) is not working. I think it thinks it’s a sting.
So I tried casting it to an int using, is_integer((int)$_GET[’idnum’]). Then whatever I threw in the _GET variable passed as an integer, even strings.
Manual says:

"To test if a variable is a number or a numeric string (such as

form
input,
which is always a string), you must use is_numeric()."

In any case, for most apps it’s enough with

(int)$_GET[’idnum’]; of
course,
using that value in your calculations, not only to validate. The

worst
problem you can face is that actual value is "3a" or "3.25"

and you get a
3.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la
intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la

papelera
-+ I’m not a free help desk, please don’t e-mail me

your questions
--</font>


you can test integer this way

preg_match("/^\d+$/", $input)

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-validate...ict146910.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=491964
Jul 17 '05 #4

P: n/a
"NotGiven" wrote:
I need to check the $_GET[’idnum’] to make certain it is a
positive integer.

is_integer($_GET[’idnum’]) is not working. I think it
thinks it’s a sting.
So I tried casting it to an int using,
is_integer((int)$_GET[’idnum’]).
Then whatever I threw in the _GET variable passed as an integer, even strings.

Any ideas or links or functions are much appreciated!


As I responded to your other post..

preg_match("/^\d+$/", $inputvalue)

of course, "0" would also eval to True here.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-validate...ict146910.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=491966
Jul 17 '05 #5

P: n/a
*** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:25:19 -0400):
right, and thanks. '3a' is exactly what I am trying to test for - I am
posting another main topic to cover this


That's the sort of situations where a regular expression (used in
preg_match) is the easiest way:

/^[0-9]{1,2}[a-z]{1}$/ ---> 1 or 2 digits plus exactly 1 letter
/^[0-9]{1}[a-z]{0,2}$/ ---> exactly 1 digit plus 0, 1 or 2 letters

etc.

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #6

P: n/a
thanks

"steve" <Us************@dbForumz.com> wrote in message
news:41**********@alt.athenanews.com...
"NotGiven" wrote:
right, and thanks. '3a' is exactly what I am trying to
test for - I am
posting another main topic to cover this
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in
message
news:x4***************************@40tude.net...
*** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):
> I need to check the $_GET['idnum'] to make

certain it is a positive
> integer.
>
> is_integer($_GET['idnum']) is not working. I

think it thinks it's a
> sting.
> So I tried casting it to an int using,

is_integer((int)$_GET['idnum']).
> Then whatever I threw in the _GET variable passed as an

integer, even
> strings.

Manual says:

"To test if a variable is a number or a numeric string (such as

form
input,
which is always a string), you must use is_numeric()."

In any case, for most apps it's enough with

(int)$_GET['idnum']; of
course,
using that value in your calculations, not only to validate. The

worst
problem you can face is that actual value is "3a" or "3.25"

and
you get a
3.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la
intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la

papelera
-+ I'm not a free help desk, please don't e-mail me

your questions
--</font>


you can test integer this way

preg_match("/^\d+$/", $input)

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL:
http://www.dbForumz.com/PHP-validate...ict146910.html
Visit Topic URL to contact author (reg. req'd). Report abuse:
http://www.dbForumz.com/eform.php?p=491964

Jul 17 '05 #7

P: n/a
thanks

"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message
news:ck*****************************@40tude.net...
*** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:25:19 -0400):
right, and thanks. '3a' is exactly what I am trying to test for - I am
posting another main topic to cover this


That's the sort of situations where a regular expression (used in
preg_match) is the easiest way:

/^[0-9]{1,2}[a-z]{1}$/ ---> 1 or 2 digits plus exactly 1 letter
/^[0-9]{1}[a-z]{0,2}$/ ---> exactly 1 digit plus 0, 1 or 2 letters

etc.

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la
intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.