how to validate $_GET var is a positive integer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NotGiven

    how to validate $_GET var is a positive integer?

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

    is_integer($_GE T['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!


  • Alvaro G. Vicario

    #2
    Re: how to validate $_GET var is a positive integer?

    *** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):[color=blue]
    > I need to check the $_GET['idnum'] to make certain it is a positive integer.
    >
    > is_integer($_GE T['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.[/color]

    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
    --

    Comment

    • NotGiven

      #3
      Re: how to validate $_GET var is a positive integer?

      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" <kAlvaroNOSPAMT HANKS@terra.es> wrote in message
      news:x455s1g3ln sj.gmxzqncs8qmw .dlg@40tude.net ...[color=blue]
      > *** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):[color=green]
      >> I need to check the $_GET['idnum'] to make certain it is a positive
      >> integer.
      >>
      >> is_integer($_GE T['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.[/color]
      >
      > 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
      > --[/color]


      Comment

      • steve

        #4
        Re: Re: how to validate $_GET var is a positive integer?

        "NotGiven" wrote:[color=blue]
        > 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" <kAlvaroNOSPAMT HANKS@terra.es> wrote in
        > message
        > news:x455s1g3ln sj.gmxzqncs8qmw .dlg@40tude.net ...[color=green]
        > > *** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):[color=darkred]
        > >> I need to check the $_GET[’idnum’] to make[/color][/color]
        > certain it is a positive[color=green][color=darkred]
        > >> integer.
        > >>
        > >> is_integer($_GE T[’idnum’]) is not working. I[/color][/color]
        > think it thinks it’s a[color=green][color=darkred]
        > >> sting.
        > >> So I tried casting it to an int using,[/color][/color]
        > is_integer((int )$_GET[’idnum’]).[color=green][color=darkred]
        > >> Then whatever I threw in the _GET variable passed as an[/color][/color]
        > integer, even[color=green][color=darkred]
        > >> strings.[/color]
        > >
        > > Manual says:
        > >
        > > "To test if a variable is a number or a numeric string (such as[/color]
        > form[color=green]
        > > input,
        > > which is always a string), you must use is_numeric()."
        > >
        > > In any case, for most apps it’s enough with[/color]
        > (int)$_GET[’idnum’]; of[color=green]
        > > course,
        > > using that value in your calculations, not only to validate. The[/color]
        > worst[color=green]
        > > problem you can face is that actual value is "3a" or "3.25"[/color][/color]
        and[color=blue]
        > you get a[color=green]
        > > 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[/color]
        > papelera[color=green]
        > > -+ I’m not a free help desk, please don’t e-mail me[/color]
        > your questions[color=green]
        > > --</font>[/color][/color]

        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

        Comment

        • steve

          #5
          Re: how to validate $_GET var is a positive integer?

          "NotGiven" wrote:[color=blue]
          > I need to check the $_GET[’idnum’] to make certain it is a
          > positive integer.
          >
          > is_integer($_GE T[’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,[/color]
          even[color=blue]
          > strings.
          >
          > Any ideas or links or functions are much appreciated![/color]

          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

          Comment

          • Alvaro G. Vicario

            #6
            Re: how to validate $_GET var is a positive integer?

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

            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
            --

            Comment

            • NotGiven

              #7
              Re: Re: how to validate $_GET var is a positive integer?

              thanks

              "steve" <UseLinkToEmail @dbForumz.com> wrote in message
              news:413d31ae$1 _3@alt.athenane ws.com...[color=blue]
              > "NotGiven" wrote:[color=green]
              > > 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" <kAlvaroNOSPAMT HANKS@terra.es> wrote in
              > > message
              > > news:x455s1g3ln sj.gmxzqncs8qmw .dlg@40tude.net ...[color=darkred]
              > > > *** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:04:11 -0400):
              > > >> I need to check the $_GET['idnum'] to make[/color]
              > > certain it is a positive[color=darkred]
              > > >> integer.
              > > >>
              > > >> is_integer($_GE T['idnum']) is not working. I[/color]
              > > think it thinks it's a[color=darkred]
              > > >> sting.
              > > >> So I tried casting it to an int using,[/color]
              > > is_integer((int )$_GET['idnum']).[color=darkred]
              > > >> Then whatever I threw in the _GET variable passed as an[/color]
              > > integer, even[color=darkred]
              > > >> strings.
              > > >
              > > > Manual says:
              > > >
              > > > "To test if a variable is a number or a numeric string (such as[/color]
              > > form[color=darkred]
              > > > input,
              > > > which is always a string), you must use is_numeric()."
              > > >
              > > > In any case, for most apps it's enough with[/color]
              > > (int)$_GET['idnum']; of[color=darkred]
              > > > course,
              > > > using that value in your calculations, not only to validate. The[/color]
              > > worst[color=darkred]
              > > > problem you can face is that actual value is "3a" or "3.25"[/color][/color]
              > and[color=green]
              > > you get a[color=darkred]
              > > > 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[/color]
              > > papelera[color=darkred]
              > > > -+ I'm not a free help desk, please don't e-mail me[/color]
              > > your questions[color=darkred]
              > > > --</font>[/color][/color]
              >
              > 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[/color]


              Comment

              • NotGiven

                #8
                Re: how to validate $_GET var is a positive integer?

                thanks

                "Alvaro G. Vicario" <kAlvaroNOSPAMT HANKS@terra.es> wrote in message
                news:ckpjfdx3yk mf.16xtcsu8uxr0 m$.dlg@40tude.n et...[color=blue]
                > *** NotGiven escribió/wrote (Mon, 6 Sep 2004 18:25:19 -0400):[color=green]
                >> right, and thanks. '3a' is exactly what I am trying to test for - I am
                >> posting another main topic to cover this[/color]
                >
                > 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
                > --[/color]


                Comment

                Working...