Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

problem with is_numeric() or I am using it wrong

Recommended Posts

[code]
<?php
if (isset($price)) {
if (!is_numeric($price)) {
$errorhandler .= "The Price value has to be Numbers only.<br />";
}
}
}
?>
[/code]
[b]Intended Code Function[/b]
[list]
[*]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.
[/list]
[b]Current Script Function[/b]
[list]
[*]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.
[/list]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I think this is a job for a Regular expression.

Look at the manual for both [url=http://uk.php.net/manual/en/function.preg-match.php]preg_match[/url] and [url=http://uk.php.net/manual/en/function.preg-replace.php]preg_replac[/url]e.

Regards
Huggie

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Id try something like:

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

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.