Page 1 of 1
!is_numeric function
Posted: Wed Oct 08, 2003 7:37 am
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
Posted: Wed Oct 08, 2003 7:47 am
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
Posted: Wed Oct 08, 2003 7:57 am
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..
Posted: Wed Oct 08, 2003 8:01 am
by Nay
Yeah, sorry about that. I added the str_replace to get rid of the spaces then check if is_numeric
-Nay
Posted: Wed Oct 08, 2003 3:24 pm
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()