PDA

View Full Version : Easy Question - How to Validate A Number is Entered?


John_Saunders
10-30-2002, 11:41 PM
I have a form that I am using and I would like to make sure the visitor enters a number for a certain field. Can somebody tell me what is wrong with my code and please explain what needed to be changed to make it work?

Here's what I have:

function isNum($number) {
return preg_match('.^\d*', $number);
}

Then in my error printing section I have:

if (isNum($_POST['number'])) {
$error .= "Error with number\n";
}

It is just validating that something is entered instead of making sure a number is entered.

Spookster
10-31-2002, 12:05 AM
Why don't you just use the php function is_numeric() ?

http://www.php.net/manual/en/function.is-numeric.php

John_Saunders
10-31-2002, 12:07 AM
I'm not that good at PHP yet. How would I use is_numeric with my error checking routine?

Thanks,

John

Spookster
10-31-2002, 12:34 AM
if (!is_numeric($_POST['number'])) {
$error .= "Error with number\n";
}

John_Saunders
10-31-2002, 12:54 AM
Thanks Spookster!


John