Page 1 of 1
Numeric textbox value
Posted: Wed Sep 17, 2003 4:59 pm
by Vannessa
Hi,Im new to Php and would like to know
how I can make sure a variable inputted is
an integer and not a chararacter.
On this form,the "score" textbox should
only have an integer typed in.
<form action="tally.php" method="post">
Score :
<input type="text" name="score">
<br/>
<input type="submit" name="Submit" value="Submit">
</p></form>
How would I verify this on the tally.php page,once
I have pressed submit?
Thank You.
Posted: Wed Sep 17, 2003 5:21 pm
by Unipus
In javascript:
Code: Select all
function isNotANumber(s) {
if (!s == "") {
return (isNaN(s))
} else {
return true;
}
}
// push is a quite useful method of arrays in newer
// javascript implementations, but not in ie5-
Array.prototype.push = function(v) {
thisїthis.length] = v
return v
}
function validate() {
var errors = ї];
if (isNotANumber(document.getElementById('score').value)) {
errors.push("Please enter the score in numbers only.");
}
// include other form-field validation here if you want.
if (errors.length > 0) {
alert(errors.join("\n"))
return false;
} else {
return true;
}
}
And then on your form:
Code: Select all
<form action="tally.php" method="post" onsubmit="return validate()">
Score :
<input type="text" name="score">
<br/>
<input type="submit" name="Submit" value="Submit">
</p></form>
This script will pop-up an alert box if the user enters anything but a number in the score field, and won't even allow the form to submit to PHP unless they fix it. If you had other fields you wanted to error check, you could include them inside the validate() function as well and they would all check onsubmit, returning a list in the alert box of fields not completed correctly. You could do some rather sophisticated client-side operations with this info if you wanted to. This is based on ypClientValidate by Aaron Brodman.
Posted: Wed Sep 17, 2003 6:02 pm
by JAM
Or you can use is_integer()
Code: Select all
if (is_integer($value)) { echo 'Yes, it is...'; }

Posted: Thu Sep 18, 2003 5:27 am
by twigletmac
JAM wrote:Or you can use is_integer()
Unfortunately not if the information is submitted via a form as posted info is considered to be a string.
I tend to typecast the data and then work with it, basically if you change something to an integer that isn't the result of the code below will be zero:
Code: Select all
$integer_value_of_form_data = (int)$form_data;
Mac
Posted: Thu Sep 18, 2003 2:33 pm
by m3rajk
on top of that anything client side can be worked around.
to do it in php, using a self processing form, here's a solution that simply has one text box.
Code: Select all
<?php
$age=NULL; # default age
$err=TRUE; # defaults to an error
if(isset($_POST['my_age'])){ $age=$_POST['my_age']; } # set the age if it was passed
if(is_int($age)){ $err=FALSE; } # undoes the default if the variable was passed and is a number
if($err){ # there was an error
echo <<<END
<html>
<!-- add html head info -->
<body>
<form action="{$_SERVER['PHP_SELF']}" method="POST">
what is your age? <input type="text" name="my_age">
<br /><input type="submit">
</body>
</html>
END;
}else{
echo <<<END
<html>
<!-- add html head info -->
<body>
how do you like being $age years old?
</body>
</html>
END;
}
?>
Posted: Thu Sep 18, 2003 9:01 pm
by Vannessa
I found using the is_numeric function() works fine so far.
Thanks for all your help.
That's one thing about php,variables aren't type defined at
the start.I think I'll start using classes soon.Maybe that will
take care of it.

Posted: Fri Sep 19, 2003 1:55 pm
by m3rajk
if you're not using a self processing form you might want to switch to one. makes displaying the form easier when there is an error