Forum Moderators: coopster

Message Too Old, No Replies

Making a input but numerical only

         

big_jimmi

1:00 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Hi guys

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!

big_jimmi

1:01 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Sorry I ment input box in the title!

dreamcatcher

1:24 pm on Jan 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi big_jimmi,

The character class [:digit:] is ideal for this:


if (ereg("^[[:digit:]]+$",$_POST['telephone']))
{
//ok
}

dc

whoisgregg

1:25 pm on Jan 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you really are validating a phone number you probably want to allow -.() and other such nonsense. :) Try a search for "php phone number validation" at your favorite search engine to get some solutions.

Edited I'm silly, is_numeric would not be the answer. Still not enough coffee this morning. :/

Nutter

1:52 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Are you trying to format it after the form has been submitted or block the user from typing non-numerics at all. If you're trying to block the entry you'll need something client side like Javascript.

henry0

2:09 pm on Jan 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// ********* Phone

$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>

big_jimmi

4:43 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Hi guys

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

eelixduppy

4:47 pm on Jan 19, 2007 (gmt 0)



This should work:

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! :)

big_jimmi

6:01 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



its worked! your a star! thanks again