Is_Numerical String Function ?

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
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Is_Numerical String Function ?

Post by HUWUWA »

Hi guys, what's the best way to test if a string contains only numbers (0...9) ? Should I use the string comparison functions or the preg_match ones ? I learn best from example and could really use one. Thanks.
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post by HUWUWA »

I figured it out. Here is a cool way to check someone's age if they decide to enter it:

$strAge="None Specified";
// frmAge is the name I used for the age in the <FORM>
if($_POST['frmAge'] != ""){
if(preg_match("([0-9])",$_POST['frmAge']) == 0)
{
$strAge="Invalid Entry";
}
else{
$strAge=$_POST['frmAge'];
}
}

// you can also do a strlen() test to see if it's more than 2 or 3 characters

I just thought I'd chip in after all the help you guys have given me.

I Love PHP !
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

or is_numeric($age);
Post Reply