If statement to check for decimal in form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Az_Critter
Forum Newbie
Posts: 11
Joined: Wed Oct 28, 2009 8:35 am

If statement to check for decimal in form?

Post by Az_Critter »

Hello all,

My problem is that I have a form in which players can enter an amount of energy to spend, but it is intended for whole numbers only. They are able to enter decimals for the amount of energy to spend. How can I write an if statement, which will check whether the number entered is whole or decimal, and echo back an error if they enter a decimal number?

Thank you very much for your time,
Az_Critter :)
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: If statement to check for decimal in form?

Post by bradbury »

Code: Select all

$string;
if(strpos($string, ".")){
  echo "error!";
}
else {
continue;
}
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: If statement to check for decimal in form?

Post by bradbury »

Actually I just remembered an easier way to do this is just to call the
is_int() function
Az_Critter
Forum Newbie
Posts: 11
Joined: Wed Oct 28, 2009 8:35 am

Re: If statement to check for decimal in form?

Post by Az_Critter »

Haha Figures it would be that simple! Thank you very much! :D
Az_Critter
Forum Newbie
Posts: 11
Joined: Wed Oct 28, 2009 8:35 am

Re: If statement to check for decimal in form?

Post by Az_Critter »

Hmmm... I'm not familiar with that function. I'll have to research it, but for now I'll use your first idea, as I know I can make that work. Thanks again. :D
Az_Critter
Forum Newbie
Posts: 11
Joined: Wed Oct 28, 2009 8:35 am

Re: If statement to check for decimal in form?

Post by Az_Critter »

It works perfectly! I stuck it into my existing code so that it looks like this;

if (is_numeric($repeat) != true || $repeat < 1 || strpos($repeat, ".")) {
echo "You must enter a positive, numeric, whole value.";
echo "<p><a href=gdragonfly.php>Try Again</a>";
include("bottom.php");
exit;}

Now when they try to enter a decimal, it automatically echoes the statement!
Thank you very much! :D
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: If statement to check for decimal in form?

Post by bradbury »

yeah no problem. good luck with your site
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: If statement to check for decimal in form?

Post by shawngoldw »

Something to note with strpos, say you use:

Code: Select all

if(strpos($str, '.'))
Take these 2 cases:
a) $str = '0.123'
b) $str = '.123'

In case a strpos will return 1 and the statement will evaluate to true.
In case b strpos will return 0 and the statement will evaluate to false.

Instead we can use

Code: Select all

if(strpos($str, '.') !== false)
This will solve the problem as strpos returns false if no match is found.


Shawn
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: If statement to check for decimal in form?

Post by AbraCadaver »

bradbury wrote:Actually I just remembered an easier way to do this is just to call the
is_int() function
Also, get and post vars are always strings, so is_int() will always return false no matter what the number is inside the string.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: If statement to check for decimal in form?

Post by bradbury »

abra is completely right regarding GET and POST. But i figured that there would be code to change the string to an integer if we were going to test it for a decimal.
That's my bad assuming is never the way to go
Az_Critter
Forum Newbie
Posts: 11
Joined: Wed Oct 28, 2009 8:35 am

Re: If statement to check for decimal in form?

Post by Az_Critter »

Thanks everyone for all the wonderful advice. I'll keep it all in mind, and research all of this as I find time. For now though, I've tested the code as I have it, and it's working perfectly in both a and b cases mentioned above. Thanks again! :D
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: If statement to check for decimal in form?

Post by requinix »

Post Reply