Page 1 of 1

Number only string detection

Posted: Mon Jul 17, 2006 8:26 am
by mjaywoods
Hi, I need to be able to detect if someone as not typed in numbers in a input field when its been submitted, can this be done?

I guess you'd something like

Code: Select all

<?php

// this would be $_POST[] etc not $date =

$date = "1";


if (ereg ("([0-9]{1,2})", $date, $regs)) {
   echo "$regs[1]";
} else {
   echo "Invalid date format: $date";
}
?>
but i can't get this to work any ideas??

thanks

Mjay

Posted: Mon Jul 17, 2006 8:28 am
by Benjamin
If you only need to verify that it is an integer you may want to use..

Code: Select all

if (!ctype_digit($_POST['variableName'])) {
    echo 'INVALID!';
}

Thanks

Posted: Mon Jul 17, 2006 8:40 am
by mjaywoods
Thanks thats the thing i needed just couldn't find it, problem solved

Posted: Mon Jul 17, 2006 9:57 am
by pickle
You could use is_numeric() too.