Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Check for Positive Integer entry only

by kyle (Abbot)
on May 09, 2007 at 17:33 UTC ( #614458=note: print w/replies, xml ) Need Help??


in reply to Check for Positive Integer entry only

You might want to look at Regexp::Common::number in general, but I don't see anything ready-made to do what you want. Here's a pattern I might use:

$dataEntry =~ m{ \A # beginning of string \+? # optional plus sign \d+ # one or more digits \z # end of string }xms

Using Regexp::Common, that might be:

use Regexp::Common qw( number ); $dataEntry =~ /$RE{num}{int}/ && $dataEntry == abs $dataEntry

If you want to allow for white space in your data entry, you could add \s* into your patterns.

Update: After consideration of the contributions of the other monks, I think this is a better expression than the one I have above:

m{ \A # beginning of string \+? # optional plus sign [0-9]* # optional digits, including zero [1-9] # mandatory non-zero digit [0-9]* # optional digits, including zero \z # end of string }xms

This incorporates Nkuvu's observation that a value of zero should not be allowed and mreece's suggestion that \d is not always the best way to match digits (though I find this counterintuitive). It also passes my own test suite.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://614458]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2023-09-25 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?