I need to detect whether a value input by a user contains a positive, non-zero number. The input field represents a product quantity, and must be greater than zero, and contain no alpha or non-numeric characters. IOW, the input must contains only these characters: 0123456789 But of course, zero by itself is not acceptable. Here’s how I am using the code:
if( $fields{'quantity'} =~ [this is where I am unsure] )
{
$errors .= "Please enter a whole number for the quantity.";
}
Thanks.
Remember that strings like
1E4are also numeric, so not every number has to contain[0-9]only.The
looks_like_numberfunction provided byScalar::Utilis the Right Way to check if a variable is numeric.The same thing more succinctly:
Be warned that strings like
Nan,InfandInfinityare also deemed numeric, so you may want to consider weeding those out as well: