function that can return true, if only number are inside it.
Moderator: General Moderators
-
anthony88guy
- Forum Contributor
- Posts: 246
- Joined: Thu Jan 20, 2005 8:22 pm
function that can return true, if only number are inside it.
I have a textbox that needs to have only numbers, and i want to check for anyother other characters.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
function checkNums($input) {
if (preg_match('/[^\d]/s', $input) {
return false;
} else {
return true;
}
}-
anthony88guy
- Forum Contributor
- Posts: 246
- Joined: Thu Jan 20, 2005 8:22 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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"
is_nan() ==> "Is Not A Number"
-
anthony88guy
- Forum Contributor
- Posts: 246
- Joined: Thu Jan 20, 2005 8:22 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
So the number must be 7-8 digits long, with no spaces, or anything else?
Or using is_nan() [See why I use a preg_match instead?]
Code: Select all
function checkNum($input) {
if (preg_match('/^\d{7,8}$/', $input)) {
return true;
} else {
return false;
}
}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;
}
}-
anthony88guy
- Forum Contributor
- Posts: 246
- Joined: Thu Jan 20, 2005 8:22 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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})) {
//...- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Code: Select all
if ($password{4} != "") {
return true;
}Code: Select all
if (strlen($password) > 4) {
return true;
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Well, you can't exactly say, "Hey! $string will always be a string and never be anything but a string!"
But you can:
http://us4.php.net/language.types.type-juggling
But you can:
Code: Select all
$integer = 23;
$string = (string) $integer;
var_dump($string);
//Returns str(2) = "23"