0

Hi, I have just started php. I have a contact form on my website and am using the is_numeric function to check that the phone number entered is a number.

What I want to do is, if the phone number is not a number to output a message just underneath the contact form on the html page telling the user they need to enter a valid number.

Is there a way to do this?

7
Contributors
7
Replies
25
Views
6 Years
Discussion Span
Last Post by stekmil
0

Try the following:

$phone = (int)$_POST['phone'];
if ($phone==0) {
//invalid phone number
} else {
echo $phone;
}
0

You can try something like the following where $num is the check that you do to check the phone number:

if (!$num){
	echo "This is not a valid phone number";
  }

Please post the code that you are using for a more detailed explanation.

Edited by trilithon: n/a

0
<html>
<head>
<body onload="msg()">
<script type="text/javascript">

function msg()
{
    alert("wrong pass............................");
}

</script>
</body>
</head>
</html>

Edited by mike_2000_17: Fixed formatting

0

if i got your problem right when user submits a from u check if number is proper on same page and if error display an error ?
if yes then try javascript

else cwarn123's solution should do

0

Checking a phone number is an integer could be problematic. Some start with 0 (not a problem in itself), some may have () or '-' included. Are these deemed invalid?

Ensure that you don't store them in a DB as integers though - you'll lose the preceeding 0 if there is one.

0

Checking a phone number is an integer could be problematic. Some start with 0 (not a problem in itself), some may have () or '-' included. Are these deemed invalid?

Ensure that you don't store them in a DB as integers though - you'll lose the preceeding 0 if there is one.

Well the long answer short to that those numbers with () and - will be valid. So what it does is basically filter all non numeric characters then returns the resulting string as an integer when using the (int) parameter and if it's an empty string then it returns 0. If however you want to echo formatted phone numbers this would be rather different and more like the following:

function validate_phone($in) {
$in=str_split($in,1);
$chars=array('0'=>true, '1'=>true, '2'=>true, '3'=>true, '4'=>true, '5'=>true, '6'=>true, '7'=>true, '8'=>true, '9'=>true, '('=>true, ')'=>true, '-'=>true, ' '=>true);
$res='';
foreach ($in AS $chr) {
if (isset($chars[$chr])) {
$res.=$chr;
}
}
return $res;
}

function invalid_phone($in) {
$in=str_split($in,1);
$chars=array('0'=>true, '1'=>true, '2'=>true, '3'=>true, '4'=>true, '5'=>true, '6'=>true, '7'=>true, '8'=>true, '9'=>true);
$res='';
foreach ($in AS $chr) {
if (isset($chars[$chr])) {
$res.=$chr;
}
}
return empty($res);
}
$_POST['phone']='(07) 54261746';
$phone = validate_phone($_POST['phone']);
if (invalid_phone($phone)==true) {
//invalid phone number
} else {
echo $phone;
}

So if your using non formatted numbers use my first example but if however the numbers may be formatted then use the above code.

0

PHP

<?php
$phone = trim($_POST['phone']);

if(!is_numeric($phone))
    $alert = 'invalid phone number';

?>

Javascript

<input type="text" id="phone" /><br />
<button type="button" id="btn_phone" onclick="control()"></button>




<script>
function IsNumeric(phone) {
   return (phone - 0) == phone && phone.length > 0;
}
function control() {
   var phone = document.getElementById('phone');
   var value = phone.value.replace(/^\s+|\s+$/g,'');
   phone.value = value;
   if (IsNumeric(value) == false)
        alert('invalid phone number');
}
</script>

Edited by stekmil

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.