Page 1 of 1
If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 11:40 am
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

Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 11:51 am
by bradbury
Code: Select all
$string;
if(strpos($string, ".")){
echo "error!";
}
else {
continue;
}
Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 11:53 am
by bradbury
Actually I just remembered an easier way to do this is just to call the
is_int() function
Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 11:53 am
by Az_Critter
Haha Figures it would be that simple! Thank you very much!

Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 11:54 am
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.

Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 12:05 pm
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!

Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 12:07 pm
by bradbury
yeah no problem. good luck with your site
Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 12:20 pm
by shawngoldw
Something to note with strpos, say you use:
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
This will solve the problem as strpos returns false if no match is found.
Shawn
Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 12:22 pm
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.
Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 12:32 pm
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
Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 5:54 pm
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!

Re: If statement to check for decimal in form?
Posted: Tue Aug 31, 2010 7:17 pm
by requinix