!is_numeric 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
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

!is_numeric function

Post by jamrop »

hey

I am making a form, and one of my fields is telephone number.
I have a check below to make sure it contains numbers.
It works if i put e.g. 12343212 but does not work if i put 01442 847392.
Is there a way to make it work?




Code: Select all

<?php
if (!is_numeric($_POST['homtel']))

{$errmsg.="Your Home Telephone number must contain numbers"; $valid=False;}
?>
Many thanks
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Yeah, since with spaces, it's not a number anymore. But you might also want to check for somethings. Maybe the length?

Code: Select all

<?php

function check($num) {
$num = str_replace(" ", "", $num); // gets rid of the spaces
if(!empty($num) && is_numeric($num) && strlen($num) == "8") { // checks if is not empty, a number and the length is 8 numbers
return TRUE; // returns true
} else {
return FALSE; // returns false, you can die() the script or something then
}
}

$no = $_POST['homtel'];

if(check($no)) {
echo "phone number is valid!";
}

?>
That also should do fine...

-Nay
Last edited by Nay on Wed Oct 08, 2003 8:00 am, edited 1 time in total.
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

i have other code in my script to check if not empty, but it will not work if i put the area code then a space and then the phone number.. I need the area code in as well you see. but i dont think is_numeric allows a space..
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Yeah, sorry about that. I added the str_replace to get rid of the spaces then check if is_numeric

-Nay
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

break it into each section. you can check if it's a number by multiplying by one and then checking against is_long()
Post Reply