PDA

View Full Version : regular expressions


pixelwraith
05-23-2004, 06:31 AM
i am making calculations and want to check that the user has entered only digits and not just a leading zero or only a zero
this is what i have used
if ($answers[2] =~/\D/)
{
exit;
}
to check that only digits have been used but cant figure out how to incorporate leading zeros or only a zero
i tried
if ($answers[2] =~/\D^0/)
{
exit;
}
and a few other versions

can someone help out with the correct syntax? :)

dswimboy
05-23-2004, 10:48 PM
$answers[2] =~ /^0/; // will match a leading zero
if ($answers[2] == 0) // will be true if all zeroes

pixelwraith
05-24-2004, 04:03 PM
thanks!
heres another
if ($answers[2] =~ /^(0)|(\D)/)

dswimboy
05-24-2004, 06:32 PM
that would return true for leading zero numbers...at least i think it should. the caret symbol matches the beginning of a line, unless in brackets (or probably anywhere other than the beginning of a string).

pixelwraith
05-28-2004, 05:11 AM
if it checks only for leading zeros its the same as checking for 0, 00, 000 etc :)