Checking if field contains numbers

Posted on 2000-04-19
Last Modified: 2010-03-05

How can I tell if $FORM{count} only contains numbers, therefore no letters etc.

Thanks in advance,

Graeme Sandwell
Question by:graemesandwell
LVL 16

Accepted Solution

maneshr earned 100 total points
ID: 2730839
You want to check whether a string represents a valid number. This is a common problem when validating input, as in a CGI script.

This problem gets to the heart of what we mean by a number. Even things that sound simple, like integer, make you think hard about what you will accept ("Is a leading + for positive numbers optional, mandatory, or forbidden?").

You must decide what you will and will not accept. Then, construct a regular expression to match those things alone. Here are some precooked solutions  for most common cases.

## Sample value.

## Eliminate any leading and trailing spaces from the variable
$FORM{count}=~ s/^\s+//;
$FORM{count}=~ s/\s+$//;

## Now check...
if ($FORM{count}!~ /^\d+$/){ # rejects -3
  print "not a natural number\n";

if ($FORM{count}!~ /^-?\d+$/){ ## rejects +3
  print "not an integer\n";

if ($FORM{count}!~ /^[+-]?\d+$/){
  print "not an integer\n";

if ($FORM{count}!~ /^-?\d+\.?\d*$/){ ## rejects .2
  print "not a decimal number\n";

if ($FORM{count}!~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/){
  print "not a decimal number\n";
LVL 84

Expert Comment

ID: 2731686
perldoc -q "How do I determine whether a scalar is a number/whole/integer/float"

Author Comment

ID: 2737606
Thanks for all the time you put into your answer!  I was looking for a number > 0.

Thanks again!

Graeme Sandwell

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now