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?

28 of 32 people (88%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do i check for float field? User is required to enter amount of cash spend and i do not want rubbish from them other than float / int?

Sep 3rd, 2000 14:32
Ben Udall, John LYC, http://www.php.net/manual/ref.regex.php


You could probably use regular expressions to validate a dollar
amount.  For those who don't know, regular expressions are a 
powerful method for string searching and manipulation, but also have a 
steep learning curve.
If $money conatins the amount entered on the form, the expression
below will make sure it's valid.
if (ereg('^[0-9]*\\.?[0-9]{0,2}$', $money))
{
    // $money is a valid dollar amount
}
else
{
    // $money is not valid
}
As a more general variation,
ereg('^[0-9]*\\.?[0-9]*$', $money)
will return true for any float value.