need code snippet to verify input strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mairhtin O'Feannag

    need code snippet to verify input strings

    Hello,

    I'm obviously a newbie, and since I detest re-inventing the wheel, can
    someone show me/give me a snippet that will verify that an input string is
    a numeric value? I'm using DB2 and would like to verify that a string is
    integer, dec(x,y) etc. Dates I've been able to find a nice routine
    (snippet) for, but numeric values seem to elude my searches.

    Also, any suggestions on what one considers to be a *GOOD* snipet site
    would help as well. :)

    TIA,

    Mairhtin
  • Janwillem Borleffs

    #2
    Re: need code snippet to verify input strings

    Mairhtin O'Feannag wrote:
    Dates I've been able to find
    a nice routine (snippet) for, but numeric values seem to elude my
    searches.
    >
    Finds whether a variable is a number or a numeric string



    JW


    Comment

    • Mairhtin O'Feannag

      #3
      Re: need code snippet to verify input strings

      "Janwillem Borleffs" <jw@jwscripts.c omwrote in news:451ad4ab$0 $20623
      $dbd4d001@news. euronet.nl:
      Okay, that's a good resource, and a good start. But when testing
      various values, I get the output :

      value of x is:1
      value of id is:222
      value of id is:


      where I would expect x and the second id to be 0, not blank. The test,
      when it succeeds, displays 1, but when it fails, it displays nothing.
      Test :

      $testamt = $_POST["amount"];
      $x = is_numeric($tes tamt);
      echo "</br>";
      echo "value of x is:";
      echo $x;

      $id = (int) $_POST["amount"];
      echo "</br>";
      echo "value of id is:";
      echo $id;

      $id = is_numeric($_PO ST["date"]);
      echo "</br>";
      echo "value of id is:";
      echo $id;

      Date is obviously not numeric, in that it contains the "/" character.

      Any thoughts?

      Mairhtin

      P.S. if you see something OBVIOUS, please be gentle, I'm a newbie!!!

      Comment

      • Colin Fine

        #4
        Re: need code snippet to verify input strings

        Mairhtin O'Feannag wrote:
        "Janwillem Borleffs" <jw@jwscripts.c omwrote in news:451ad4ab$0 $20623
        $dbd4d001@news. euronet.nl:
        >>
        Okay, that's a good resource, and a good start. But when testing
        various values, I get the output :
        >
        value of x is:1
        value of id is:222
        value of id is:
        >
        >
        where I would expect x and the second id to be 0, not blank. The test,
        when it succeeds, displays 1, but when it fails, it displays nothing.
        Test :
        >
        $testamt = $_POST["amount"];
        $x = is_numeric($tes tamt);
        echo "</br>";
        echo "value of x is:";
        echo $x;
        >
        $id = (int) $_POST["amount"];
        echo "</br>";
        echo "value of id is:";
        echo $id;
        >
        $id = is_numeric($_PO ST["date"]);
        echo "</br>";
        echo "value of id is:";
        echo $id;
        >
        Date is obviously not numeric, in that it contains the "/" character.
        >
        Any thoughts?
        >
        Mairhtin
        >
        P.S. if you see something OBVIOUS, please be gentle, I'm a newbie!!!
        PHP has a distinct type 'boolean'. 1, like all non-zero values, is
        converted to TRUE in boolean context, but it is not the same as TRUE. 0
        is converted to FALSE in boolean context, but it is not the same as FALSE.

        It would appear from your test that 'echo' reports True as '1' and False
        as blank. I cannot find anything in the on-line manual that says how it
        should behave.

        So - don't be worried that it is not reporting what you expect:

        if (is_numeric($_i d)) {
        ...
        } else {
        ...
        }

        will work fine.

        Colin

        Colin

        Comment

        • Mairhtin O'Feannag

          #5
          Re: need code snippet to verify input strings

          Thank you Colin. I was expecting 1 and 0 as true and false respectively.
          But the test works, so who am I to complain? :)

          Thanks again



          Colin Fine <news@kindness. demon.co.ukwrot e in
          news:efov32$5vd $1$8302bc10@new s.demon.co.uk:
          Mairhtin O'Feannag wrote:
          >"Janwillem Borleffs" <jw@jwscripts.c omwrote in
          >news:451ad4ab$ 0$20623 $dbd4d001@news. euronet.nl:
          >>>>
          >Okay, that's a good resource, and a good start. But when testing
          >various values, I get the output :
          >>
          >value of x is:1
          >value of id is:222
          >value of id is:
          >>
          >>
          >where I would expect x and the second id to be 0, not blank. The
          >test, when it succeeds, displays 1, but when it fails, it displays
          >nothing. Test :
          >>
          > $testamt = $_POST["amount"];
          > $x = is_numeric($tes tamt);
          > echo "</br>";
          > echo "value of x is:";
          > echo $x;
          >>
          > $id = (int) $_POST["amount"];
          > echo "</br>";
          > echo "value of id is:";
          > echo $id;
          >>
          > $id = is_numeric($_PO ST["date"]);
          > echo "</br>";
          > echo "value of id is:";
          > echo $id;
          >>
          >Date is obviously not numeric, in that it contains the "/" character.
          >>
          >Any thoughts?
          >>
          >Mairhtin
          >>
          >P.S. if you see something OBVIOUS, please be gentle, I'm a newbie!!!
          >
          PHP has a distinct type 'boolean'. 1, like all non-zero values, is
          converted to TRUE in boolean context, but it is not the same as TRUE.
          0 is converted to FALSE in boolean context, but it is not the same as
          FALSE.
          >
          It would appear from your test that 'echo' reports True as '1' and
          False as blank. I cannot find anything in the on-line manual that says
          how it should behave.
          >
          So - don't be worried that it is not reporting what you expect:
          >
          if (is_numeric($_i d)) {
          ...
          } else {
          ...
          }
          >
          will work fine.
          >
          Colin
          >
          Colin
          >

          Comment

          Working...