Jump to content


Photo

problem with is_numeric() or I am using it wrong


  • Please log in to reply
5 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 September 2006 - 04:32 PM

<?php
if (isset($price)) {
		if (!is_numeric($price)) {
			$errorhandler .= "The Price value has to be Numbers only.<br />";
			}
		}
}
?>
Intended Code Function
  • Check to see if they filled out the price field.
  • If they did make sure it was numeric value only.
  • If not return an error indicating so.
Current Script Function
  • Checks to see if they put in a price
  • Check to see if it's numeric only, but fails in it's purpose. It misses the $ symbol, so when someone puts in a price with  a $ then it returns the value always to $0 instead.  Also I have it automatically putting in a dollar sign on display, so I needed to keep them from it here.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 September 2006 - 04:38 PM

You cannot add a dollar sign and pass that through the is_numeric function . is numeric only accepts numbers, optional decimal point, optional exponential part and few other number specific characters.

First you'll wnat to loose the dollar sign and then pass it through the function and then add the dollar sign backin.

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 04:41 PM

I think this is a job for a Regular expression.

Look at the manual for both preg_match and preg_replace.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 September 2006 - 04:54 PM

No I mean, I am adding hte dollar sign when the information is pulled from the database.  I was hoping to use is_numeric to check and see if they put in a dollar sign, and return an error message.  When I first tested it, with a dollar sign it returned the error, but now it's just changing it over to a $0 instead.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 06:00 PM

Id try something like:

<?php
$price = $_GET['price'];
if (preg_match("/^\$/", $price)){
   echo "Please remove the $ from the price field<br>\n";
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 September 2006 - 07:11 PM

ah thanks, I appreciate it.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users