Results 1 to 10 of 10

Thread: PHP woes : checking if value is numeric

  1. #1
    ninja SEO paul's Avatar
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,500
    Post Thanks / Like

    Default PHP woes : checking if value is numeric

    I'm using this function from PHP: is_numeric - Manual
    PHP Code:
     function is_whole_number($var){
      return (
    is_numeric($var)&&(intval($var)==floatval($var)));

    Now this works to see if the input is a numeral. But what should I do to check if the value is set in the first place ? I tried PHP: isset - Manual isset but that didn't swing it.

    I'm sure it's something obvious I am missing. Any hints ? Thanks ?

  2. #2
    Senior Member louie's Avatar
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,423
    Post Thanks / Like

    Default

    you do the isset() before calling the function
    PHP Code:
     is_whole_number($var
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  3. #3
    ninja SEO paul's Avatar
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,500
    Post Thanks / Like

    Default

    Thanks Louie for the help

    Still having problems though
    PHP Code:
    function is_whole_number($var){
      return (
    is_numeric($var) && (intval($var)==floatval($var)) && ($var!=NULL) );
    }

    if ( isset(
    $_POST[checkit]) && is_whole_number($_POST[checkit])){
    // do
    }else {
    // no do

    still bails out when I don't have a value in my form to check.

    I have an input box that I want people to either leave blank, or enter a numeral. Otherwise if they enter 'six' or something like that I want it to report the error.
    Last edited by paul; 22-02-2007 at 09:18 PM. Reason: more info

  4. #4
    Senior Member louie's Avatar
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,423
    Post Thanks / Like

    Default

    don't get it. what format are you loonking to get from the visitor?

    like 6.00 or 6

    give an example
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  5. #5
    ninja SEO paul's Avatar
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,500
    Post Thanks / Like

    Default

    Hi Louie,

    I want to make sure that a user gives a whole decimal. The field is four characters long, and I am afraid some bright spark will enter 'five' instead of '5'. So only a whole number will do. No "two" "2,3" or "2.3" values should be accepted only 2.

  6. #6
    Senior Member louie's Avatar
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,423
    Post Thanks / Like

    Default

    try this:

    PHP Code:
     
    if (isset($_POST['checkit'])){
      
    $no_post trim($_POST['checkit']);
      if(
    is_numeric($no_post)){
        
    // do something here cause is number
      
    }else {
        
    // no do only numbers
      
    }  
    }else{
      
    //you haven't enter any numbers

    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  7. #7
    ninja SEO paul's Avatar
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,500
    Post Thanks / Like

    Default

    Thanks mate. Will check this out tomorrow. Time to call it a day here

  8. #8
    ninja SEO paul's Avatar
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,500
    Post Thanks / Like

    Default

    sorry about this I forgot to mention that if he enters a null value then that is okay.
    so logic
    Code:
     is NULL == okay proceed
     is ! NULL 
          IS NUMERIC == okay proceed
          IS NOT NUMERIC == give out error.

  9. #9
    Senior Member louie's Avatar
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,423
    Post Thanks / Like

    Default

    PHP Code:
    if (isset($_POST['checkit'])){
    $no_post trim($_POST['checkit']);
    if(
    is_numeric($no_post)){
    // do something here cause is number
    }else {
    // no do only numbers


    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  10. #10
    ninja SEO paul's Avatar
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,500
    Post Thanks / Like

    Default

    Cheers for your help louie and pointing me in the right direction.

    The problem was checking if something was NULL . I used
    PHP Code:
    function is_whole_number($var){
      return (
    is_numeric($var)&&(intval($var)==floatval($var)));
    }


    function 
    postvars() {
       foreach(
    func_get_args() as $var) {
      
    // echo $var. " ". $_POST[$var]." <br/>" ;
           
    if( $_POST[$var] === '' || is_whole_number($_POST[$var])) return true;
       }
       return 
    false;
    }

    $happy = (postvars('checkit')); 

Similar Threads

  1. PHP on windows
    By louie in forum Coding Help
    Replies: 20
    Last Post: 22-02-2006, 05:25 PM

Visitors found this page by searching for:

php is not numeric

php isnumeric

php not numeric

php is numeric

isnumeric php

php if not numeric

not numeric php

php check if value is a number

php check numeric valuecheck if value is a number phpphp check if a value is a numberPHP check if value is numberis numeric phpphp if numericcheck if value is numeric phpcheck numeric value in phphow to check numeric value in phpphp test if value is numericphp check value is numericphp check if value numberrelated:davidwalsh.namephp-validatie-numeric-digits is numeric function in phpphp check value numericPHP test for numeric valuephp if isset numericphp _post is numeric

Tags for this Thread

Posting Permissions

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