Page 1 of 1
Integer Validation
Posted: Sun Mar 30, 2003 11:00 am
by Eugenia
<?php
if ($submit == "Calculate here"){
if (($diameter == "") || ($spindle == "")){
if(('A' >= $diameter && $diameter <= 'Z') || ('a' >= $diameter && $diameter <= 'z') || ('A' >= $spindle && $spindle <= 'Z') || ('a' >= $spindle && $spindle <= 'z')){
echo "<font color=red>Please enter numbers only.</font>";
}
else{
echo "<font color=red>Please enter a value!</font>";
}
}
else {
$value1 = trim("$diameter");
$value2 = trim("$spindle");
$result = $value1 * ($value2 * (22/7));
//print results
print("The Cutting Speed, V will be</font> ");
printf('%.2f', $result);
print(" m/min.");
}
}
?>
My validation isn't working right.
Using a form, I'm capturing 2 variables. $diameter and $spindle.
Using the 2 variables, they are calculated using the formula above, and output the result.
However, I can't validate correctly. As in.. the statements don't appear at the right time.
For example, if both variables do not have a value, and I click "submit", the error statement would be "Please enter numbers only." instead of "Please enter a value!".
Please help! Thanks!

Posted: Sun Mar 30, 2003 1:38 pm
by twigletmac
Try this:
Code: Select all
<?php
if (!empty($_POST['submit']) && $_POST['submit'] == 'Calculate here'){
if ($_POST['diameter'] == '' && $_POST['spindle'] == '') {
echo '<font color="red">Please enter a value!</font>';
} elseif (!is_numeric($_POST['diameter']) || !is_numeric($_POST['spindle'])) {
echo '<font color="red">Please enter numbers only.</font>';
} else {
$value1 = trim($_POST['diameter']);
$value2 = trim($_POST['spindle']);
$result = $value1 * ($value2 * (22/7));
//print results
print 'The Cutting Speed, V will be</font>';
printf('%.2f', $result);
print ' m/min.';
}
}
?>
Basically you can use
is_numeric() to test to see if what's been entered is a number. Another thing you might want to do is to use $_POST (PHP version 4.1 and up) or $HTTP_POST_VARS (PHP 4.0.6 or below) if you are using the POST method for the form or $_GET or $HTTP_GET_VARS if you are using the GET method as this will make your script a bit more future proof:
viewtopic.php?t=511
Mac
Posted: Mon Mar 31, 2003 9:10 am
by Eugenia
thanks twigletmac.
I've replaced my code with yours, tested it, and it's running the way I want it to!
However, there's still a little hiccup which I can't solve while trying to fiddle with the statements and ifelse conditions.
Here's the hiccup:
Whenever I enter a
number into only
one field, the error statement that appeared is "Please enter numbers only." instead of "Please enter a value!".
The problem is with the ifelse statements, right?
Posted: Mon Mar 31, 2003 9:44 am
by twigletmac
It should work if you change:
Code: Select all
if ($_POST['diameter'] == '' && $_POST['spindle'] == '') {
to
Code: Select all
if ($_POST['diameter'] == '' || $_POST['spindle'] == '') {
as && means AND so both arguments have to be true for the statement to execute whereas || means OR so the statement will execute if either of the arguments evaluates to true.
Mac