faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input : Validating Data

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

60 of 63 people (95%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I ensure that data entered by the user is numbers only?

May 30th, 2001 15:27
Philip Olson, Onno Benschop, Nathan Wallace,


You can test to see if there are only numbers using the very useful 
is_numeric function :
  if (is_numeric($data)) {
      print 'this is numeric alright!' ;
  }
If you're running PHP3 then using is_numeric is not an option, consider 
using some regex instead.  For example :
  if (ereg("[^[:digit:]]", $data)) {
    print 'contains illegal characters!' ;
  }
References :
  http://www.php.net/manual/function.is-numeric.php 
  http://www.php.net/manual/function.ereg.php