Integer size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beany
    New Member
    • Nov 2006
    • 173

    Integer size

    Hi,

    what function can i use to ensure that a user can only enter a integer consisting of five digits and no more? Is there a function for this?

    checked perldoc but couldnt find what im looking for..

    regards
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    There is no function although there are probably modules that provide a function you could use to validate the user input. You can use a regexp:

    Code:
    unless ($var =~ /^\d{5}$/) {
       print "No good, too many or too few digits\n";
    }

    Comment

    • Beany
      New Member
      • Nov 2006
      • 173

      #3
      thanks for this,

      im not at the level at the moment to understand the example you have provided...

      can you please explain this bit ($var =~ /^\d{5}$/)...

      thanks

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        $var is the string with the digits
        =~ is the binding operator
        / is the beginning of regexp delimiter
        ^ is the beginning of string anchor
        \d is a digit
        {5} is a quantifier meaning exactly 5
        $ is the end of string anchor
        / is the end of regexp delimiter

        google for some perl regular expressions tutorials. Regular expressions can take a long time to learn, they are definetly one of the harder things to lean in any language that supports them. The one I posted though is very basic and using nothing I would consider advanced for a regexp.

        Comment

        Working...