Forum Moderators: coopster
I'm trying to validate an income field on a web form. I'm having problems working out how to say allow 0 or 1 decimal point in the field - i.e someone could input 800 or 800.00 not 800..00. Also if someone does use a decimal point a number must follow it - ie 800.00 not 800.
This is what im using at the min (which doesnt do the above)
if ($_POST['earningsfirst'] == '') {
$error_msg['earningsfirst'] = '<div class=message>Please enter your earnings</div>';
}
if(!empty($_POST['earningsfirst'])) {
if (!preg_match('/^[0-9\.]+$/' , $_POST['earningsfirst'])){
$error_msg['earningsfirst'] = '<div class=message>Invalid character in main earnings. </div>';
}
}
any ideas?
thanks in advance
if (!ctype_digit [php.net]($_POST['earningsfirst']))
{
//error
}
or the character class [[:digit]]
if (!ereg("^[[:digit:]]+$", $_POST['earningsfirst']))
{
//error
}
dc
Ive tried ctype_digit and [:digit:} but i need to allow for the user inputting income with and without a decimal point - which i can do BUT I need to make sure if an decimal point is used then only one can be inputted and it must be followed by a number ie
800 , 800.00
NOT
800. , 8.00.0 , 8.0.
Really struggling with this - any more ideas?
Thanks