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.
Numeric textbox value
Moderator: General Moderators
In javascript:
And then on your 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.
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;
}
}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>Or you can use is_integer()

Code: Select all
if (is_integer($value)) { echo 'Yes, it is...'; }- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Unfortunately not if the information is submitted via a form as posted info is considered to be a string.JAM wrote:Or you can use is_integer()
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;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.
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;
}
?>