Irish SEO,  Marketing & Webmaster Discussion

 

PHP woes : checking if value is numeric

This is a discussion on PHP woes : checking if value is numeric within the Coding Help forums, part of the Webmaster Help category; I'm using this function from PHP: is_numeric - Manual PHP Code:   function  is_whole_number ( $var ){   return ( is_numeric ( ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Members List Calendar Search Today's Posts Mark Forums Read


Notices

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 22-02-2007, 09:23 PM
paul's Avatar
ninja SEO
Recent Blog: That SEO contest
 
Join Date: Dec 2006
Location: .de
Posts: 1,120
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
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 ?
__________________
my sites :
irish poker / irish jobs / seo faq / advertise jobs free / green card / skiing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 22-02-2007, 09:41 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,048
Nominated 5 Times in 3 Posts
Nominated TOTW/F/M Award(s): 1
louie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enough
Send a message via Yahoo to louie Send a message via Skype™ to louie
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
:. Directory Submission Directory Submission
:. News & Press Release Ireland GiveItSocks.com
:. Used Cars Ireland, Car Parts & Car Audio Cars For Sale, Car Parts & Accessories
:. I Have 2 Find It Directory SEF Directory
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 22-02-2007, 10:14 PM
paul's Avatar
ninja SEO
Recent Blog: That SEO contest
 
Join Date: Dec 2006
Location: .de
Posts: 1,120
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
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 10:18 PM. Reason: more info
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 22-02-2007, 10:26 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,048
Nominated 5 Times in 3 Posts
Nominated TOTW/F/M Award(s): 1
louie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enough
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

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

like 6.00 or 6

give an example
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 22-02-2007, 10:44 PM
paul's Avatar
ninja SEO
Recent Blog: That SEO contest
 
Join Date: Dec 2006
Location: .de
Posts: 1,120
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 22-02-2007, 10:50 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,048
Nominated 5 Times in 3 Posts
Nominated TOTW/F/M Award(s): 1
louie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enough
Send a message via Yahoo to louie Send a message via Skype™ to louie
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 22-02-2007, 10:59 PM
paul's Avatar
ninja SEO
Recent Blog: That SEO contest
 
Join Date: Dec 2006
Location: .de
Posts: 1,120
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
Default

Thanks mate. Will check this out tomorrow. Time to call it a day here
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 23-02-2007, 11:20 AM
paul's Avatar
ninja SEO
Recent Blog: That SEO contest
 
Join Date: Dec 2006
Location: .de
Posts: 1,120
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9 (permalink)  
Old 23-02-2007, 11:23 AM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,048
Nominated 5 Times in 3 Posts
Nominated TOTW/F/M Award(s): 1
louie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enough
Send a message via Yahoo to louie Send a message via Skype™ to louie
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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10 (permalink)  
Old 26-02-2007, 02:50 PM
paul's Avatar
ninja SEO
Recent Blog: That SEO contest
 
Join Date: Dec 2006
Location: .de
Posts: 1,120
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
paul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud ofpaul has much to be proud of
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')); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
checking, numeric, php, woes

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
PHP on windows louie Coding Help 20 22-02-2006 06:25 PM


Sponsored links

Paid On Results


All times are GMT +1. The time now is 04:30 AM.


Powered by: vBulletin Version 3.7.3, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Hosted in Ireland by Blacknight - Test your ISP |Irish Hosting Directory| Armchair.ie|Logo by Eden Web Design|Avatars by Afterglow |Latest Blog Entries | VPS HostingAd Management by RedTyger