SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    I know this seems a simple question, but I can't pinpoint the reference.

    Does anyone know how to check that a user has entered only numbers into a submitted form using php.

    The problem is that the form converts the number into a string so that the command 'is_int' does not apply.

    What I'm looking for, is probably the php equivalent of the Javascript function:

    if (!parseInt(users.cell_number.value, 10)) {

    e.t.c......

    Thank you very much if you can help.

    Regards,

    Jason

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2000
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what you can do convert value to integer or double whaever you need. Then compare it with the initial value. If they are equal everything is okey. I could not find easier way.


    $new = doubleval($org);
    if ($org==$new)
    return true;
    else
    return false;
    Great Dane
    Gokhan ARLI

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could also do it using regular expressions, but the above method looks fairly sound so I'll spare you the code.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks gents,

    I was hoping there might be a more direct (one function method) as I'm already using something similar:

    $test = "0001";
    $testcp = $test;
    settype($testcp,"integer");


    if (strval($testcp) == $test):
    echo("$test is a number");
    else:
    echo ("$test is not a number");
    endif;


    but clearly not,

    Thanks again....

    Jason

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually what kevin said is the best method here is the code:

    function num_check($num) {
    return ereg("^[0-9]+$", $num);
    }

    Sample Usage:
    if (!num_check($num)) { print "Must be integer"; }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you again Freddy!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •