Forum Moderators: coopster
Im fairly new to php and wonder if someone could point me in the right direction..
I'm trying to make a input box on my site only accept numerical values. I thought I had it with the code below - however it still lets you put letter after the numbers ie "5454dsfsf" How can I make it numerical only?
Heres the existing code
if (ereg('^[^0-9 ]+$' , $_POST['telephone'])) {
$error_msg['telephone'] = '<div class=message>Invalid character in telephone. </div>';
}
Thanks in advance!
$phone=$_POST['phone'];
if (empty($phone))
{
echo"<h3>The telephone number field is empty!</h3><p>
<a href='../#*$!#*$!.php'><b>Please try again</b></a>";
exit();
}
if (isset ($phone) &&!empty ($phone) )
{
if(!preg_match("/^[0-9\-\ ]+$/",$phone))
{
echo "The Telephone Number could ONLY contain Numerics! <br>
<b>You entered: $phone</b><br>
<a href='../#*$!#*$!X.php'><b>Please try again</b></a>";
Exit();
}
else
{
$_SESSION['phone']=$phone;
$phone=$_SESSION['phone'];
}
// ********* End of phone
<EDIT>
as far as accepting other signs
I won't do it
just have a *Warning and show an acceptable form of entering a phone mumber.
</EDIT>
Thanks for all the helpfull replies - this is what ended up working best
if (!preg_match('/^[0-9 ]+$/' , $_POST['telephone'])){
$error_msg['telephone'] = '<div class=message>Invalid character in telephone. </div>';
}
Ive tried a few ways but I cant get it to work if the field is blank - i.e if the field contains anything other than numbers display an error but if field is blank then ignore and dont display error...
Any ideas - pulling my hair out at this one!
Thanks again
if(![url=http://us3.php.net/empty]empty[/url]($_POST['telephone'])) {
if (!preg_match('/^[0-9 ]+$/' , $_POST['telephone'])){
$error_msg['telephone'] = '<div class=message>Invalid character in telephone. </div>';
}
}
Good luck! :)