Page 1 of 2

function that can return true, if only number are inside it.

Posted: Mon Mar 28, 2005 11:31 am
by anthony88guy
I have a textbox that needs to have only numbers, and i want to check for anyother other characters.

Posted: Mon Mar 28, 2005 11:36 am
by Chris Corbyn

Code: Select all

function checkNums($input) {
    if (preg_match('/[^\d]/s', $input) {
        return false;
    } else {
        return true;
    }
}

Posted: Mon Mar 28, 2005 11:38 am
by anthony88guy
their is no set function is php that can do this? Thankyou for the function.

Posted: Mon Mar 28, 2005 11:41 am
by Chris Corbyn
No, other than is_nan() but I'm not sure how well it will work with text areas spanning multiple lines.

is_nan() ==> "Is Not A Number"

Posted: Mon Mar 28, 2005 12:35 pm
by anthony88guy
The number is only 7-8 digits.

Posted: Mon Mar 28, 2005 12:48 pm
by Chris Corbyn
So the number must be 7-8 digits long, with no spaces, or anything else?

Code: Select all

function checkNum($input) {
    if (preg_match('/^\d{7,8}$/', $input)) {
        return true;
    } else {
        return false;
    }
}
Or using is_nan() [See why I use a preg_match instead?]

Code: Select all

function checkNum($input) {
    if (!is_nan($input)) {
        if (strlen($input) <= 8 && strlen($input) >= 7) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

Posted: Mon Mar 28, 2005 1:23 pm
by oceglok
How about is_numeric()?

http://jp2.php.net/is_numeric

Hope, this helps
oceglok

Posted: Mon Mar 28, 2005 1:31 pm
by anthony88guy
So the number must be 7-8 digits long, with no spaces, or anything else?
Yes nothing else. The is_numeric() should work.

Posted: Mon Mar 28, 2005 3:57 pm
by Chris Corbyn
oceglok wrote:How about is_numeric()?

http://jp2.php.net/is_numeric

Hope, this helps
oceglok
Still wont check if length is between 7 and 8 characters...

Posted: Mon Mar 28, 2005 4:08 pm
by John Cartwright
I would probably just use preg.. take it in one shot instead of multiple checks.

Posted: Mon Mar 28, 2005 5:02 pm
by Ambush Commander
Or, is_numeric, and then cast it as a string and check if $n{6} is set and if $n{9} is not set (needlessly complicated, though). But if you want to make sure, say, a string is longer than four characters, this should suffice:

Code: Select all

if(empty($string{4})) {
//...

Posted: Mon Mar 28, 2005 5:08 pm
by feyd
uh.. why not just check the return of strlen() for that?

Posted: Mon Mar 28, 2005 5:11 pm
by Ambush Commander

Code: Select all

if ($password{4} != "") {
  return true;
}
vs.

Code: Select all

if (strlen($password) > 4) {
  return true;
}
Supposedly, since {} is a language construct, there's less overhead.

Posted: Mon Mar 28, 2005 5:22 pm
by Pyrite
php supports type casting?? Sweet!! I didn't know that.

Posted: Mon Mar 28, 2005 5:36 pm
by Ambush Commander
Well, you can't exactly say, "Hey! $string will always be a string and never be anything but a string!"

But you can:

Code: Select all

$integer = 23;
$string = (string) $integer;
var_dump($string);

//Returns str(2) = "23"
http://us4.php.net/language.types.type-juggling