Numeric textbox value

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
Vannessa
Forum Newbie
Posts: 2
Joined: Wed Sep 17, 2003 4:59 pm
Location: toronto

Numeric textbox value

Post 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.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

In javascript:

Code: Select all

function isNotANumber(s) &#123;
		if (!s == "") &#123;
			return (isNaN(s))
		&#125; else &#123;
			return true;
		&#125;
	&#125;

// push is a quite useful method of arrays in newer 
	// javascript implementations, but not in ie5-
	Array.prototype.push = function(v) &#123;
		this&#1111;this.length] = v
		return v
	&#125;

function validate() &#123;

var errors = &#1111;];

if (isNotANumber(document.getElementById('score').value)) &#123;	
			errors.push("Please enter the score in numbers only.");
&#125;

// include other form-field validation here if you want.

if (errors.length > 0) &#123;
			alert(errors.join("\n"))
			return false;
		&#125; else &#123;
			return true;
		&#125;
&#125;
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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Or you can use is_integer()

Code: Select all

if (is_integer($value)) { echo 'Yes, it is...'; }
;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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;
}
?>
Vannessa
Forum Newbie
Posts: 2
Joined: Wed Sep 17, 2003 4:59 pm
Location: toronto

Post 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. :oops: :P
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Post Reply