PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,577 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,190 people online right now. Registration is fast and FREE... Join Now!




Numbers only in a textfield in php

 

Numbers only in a textfield in php

ho-ho

30 Jun, 2009 - 12:55 PM
Post #1

New D.I.C Head
*

Joined: 4 May, 2009
Posts: 39

Hi, I have a textfield on a form. I would like that the textfield to only accepts numbers.When submit button is presseed it takes to another page called test.php.
I want that if the user enters anything other than numbers,it doesn't go to test.php but stays on the same form and tells the user to enter only numbers. Can this be done in php?
Here's the code which I am trying but isn't working.Thanks
CODE


<label>Start Number:
  <input name="number" type="text" />
  <?PHP
  
    $number = $_POST['number'];
    function text_and_spaces_only($number)
    {
    return preg_match('/[0-9 ]+/i',$number);
     }
  
?>

  </label>




This post has been edited by ho-ho: 30 Jun, 2009 - 12:56 PM

User is offlineProfile CardPM
+Quote Post


CTphpnwb

RE: Numbers Only In A Textfield In Php

30 Jun, 2009 - 01:01 PM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,066



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
http://www.php.net/is_numeric
CODE
if(is_numeric($_POST['number']))
{
// do something
} else {
// reject the post
}

User is offlineProfile CardPM
+Quote Post

ho-ho

RE: Numbers Only In A Textfield In Php

30 Jun, 2009 - 01:10 PM
Post #3

New D.I.C Head
*

Joined: 4 May, 2009
Posts: 39

Thanks for the help. How command can I use to remain on the same form and not go to the other page called test.php if the user does not enters numbers.
Thanks, I am new to php.
User is offlineProfile CardPM
+Quote Post

BetaWar

RE: Numbers Only In A Textfield In Php

30 Jun, 2009 - 01:15 PM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
You could change the header location in the else statement of the code given above.
http://us2.php.net/manual/en/function.header.php

HTH
User is offlineProfile CardPM
+Quote Post

ho-ho

RE: Numbers Only In A Textfield In Php

30 Jun, 2009 - 03:50 PM
Post #5

New D.I.C Head
*

Joined: 4 May, 2009
Posts: 39

Thanks
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:18AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month