IBM developerWorks
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old August 21st, 2002, 07:24 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
Validate numbers or digits only

Hello,
I have tried to get this but...
$a=123.22;
$b="123,23";
$c="767rrrr";
$d="é45";
$e=#10;
echo (only_numbers($a))?"Valid":"Invalid";
echo (only_numbers($b))?"Valid":"Invalid";
echo (only_numbers($c))?"Valid":"Invalid";
echo (only_numbers($d))?"Valid":"Invalid";

variable $a shoud be the only one valid but it is not, it also validates $d.

function only_numbers($para)
{
if (!ereg("[a-zA-Z,]"))
{
return 1
}
}

I have also tried other things like [[:digit:]] and [0-9] but commas and letters pass through.

I just want numbers with decimal point.

Reply With Quote
  #2  
Old August 21st, 2002, 07:28 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Validate numbers or digits only

how about something like

if(is_int($val) || is_float($val))
{
// it's numerical
}

Reply With Quote
  #3  
Old August 22nd, 2002, 01:22 AM
Gipz Gipz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Stockholm, Sweden
Posts: 98 Gipz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Gipz
RE: Validate numbers or digits only

or:

php Code:
Original - php Code
  1.  
  2. <?php
  3.     function stringIsAllNumbers($string)
  4.     {
  5.         if((gettype($string)) == "integer") {
  6.             return  true;
  7.         }
  8.         $Bad = $this->stringStripNumbers($string);
  9.         if(empty($Bad)) {
  10.             return true;
  11.         }
  12.         return  false;
  13.     } // stringIsAllNumbers()
  14.  
  15.  
  16.     function stringStripNumbers($string)
  17.     {
  18.         $stripped = eregi_replace("([0-9]+)", "", $string);
  19.         return  $stripped;
  20.     } // stringStripNumbers()
  21.  
  22. $numbers = 12343;
  23. stringIsAllNumbers($numbers); // returns true or false
  24. ?>

Reply With Quote
  #4  
Old August 22nd, 2002, 05:07 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
RE: Validate numbers or digits only

Thank you for trying, but none did work.

notePad's is_int-or-is_float version returns FALSE because the variables shown somehow are treated as string when queried or posted in a form.

Gipz' more elaborated version returns all data as being invalid. The pointer $this-> should be eliminated; it is not a class (I guess..)

So the function that is likely to funtion is:
php Code:
Original - php Code
  1.  
  2. <?php
  3. function only_numbers($para)
  4. {
  5.     if (!ereg("([[:alpha:],-]+)",$para))
  6.     {
  7.         return 1;
  8.     }
  9.     return 0;
  10. }
  11. ?>

The problem is that this returns TRUE even for
$para="é45"; or "[accented-letter]45"; or "4%5";
//any special and not-so-special chars out of the "alpha" range anywhere in the string (when capturing data it is received as STRING!

Is there a way to write something like...?
php Code:
Original - php Code
  1. <?php !ereg("([chr(91)-chr(258)])",$para) ?>

...all characters out of the alpha-range; including îéç#[ (special chars)...

Reply With Quote
  #5  
Old August 22nd, 2002, 07:42 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Validate numbers or digits only

try:
php Code:
Original - php Code
  1.  
  2.  
  3. function only_numbers($para)
  4. {
  5.     return preg_match("|^[d.]+$|", $para);
  6. }
  7.  

Reply With Quote
  #6  
Old August 23rd, 2002, 10:35 AM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
RE: Validate numbers or digits only

It works!!!

Zombies' function works perfectly for my case.

THNX AGAIN!

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Validate numbers or digits only


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway