ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Checking for non-numbers in character field

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking for non-numbers in character field

    I have a character field (did you read that, character) entered by a user that should always be numbers 0-9 (numbers in a character field - whomever set this up should be drawn and quartered) but because it's a character field, sometimes they get sloppy or just decide to be a jerk that day, and they enter a letter somewhere in the field. I need to find this and throw them an error message to ruin THEIR day instead of mine.

    I tried doing a lookup using a named constant of ('0123456789') but the compiler didn't like that so i went with (0123456789) and that too, the compiler didn't like.

    Found something out there in space in FREE but I'll get the "talk" if I go that route so I'm stuck in ancient times; RPGLE.
    (trying to get into the current century but progress moves slowly sometimes.)

    Going to try the CHECK op code but in the meantime, any other ideas would be appreciated.
    Mike.
    Everyday's a school day, what grade are you in?

  • #2
    I usually go with something like this:
    Code:
    Dcl-c NUMERIC '0123456789' ;
    Dcl-s FieldName varChar(10) ;  
    
    If %check(NUMERIC :FieldName) = 0;
    Endif;

    Comment


    • #3
      jtaylor,
      Is that FREE?
      Can't do FREE - I'll get the "talk"
      Everyday's a school day, what grade are you in?

      Comment


      • jtaylor___
        jtaylor___ commented
        Editing a comment
        Sorry, I missed that part about no modern RPG. I'd imagine you can do the same thing in legacy RPG. The CHECK op-code should set a variable or indicator you can check.

    • #4
      The %Check is good if you want to error it out. But it has "gaps" i.e. if you add space to the set (which should be valid as a leading/trailing entity) then you have the issue of '123 45' being valid according to %Check. An alternative might be to use %Int (assuming this is supposed to be a whole number) and placing that in a MONITOR block. Like so:

      Code:
      Monitor;
         number = %Int( %Trim(inputField) );  // Trim to remove leading trailing blanks
      On-Error;
         // Bad entry
      EndMon;
      Use %Dec instead if decimals are valid.

      Despite the fact that this SHOULD be done in free-form it will work in the ancient ways too. Just add EVAL where needed. And of course make all the variable names upper case ...

      Comment


      • jtaylor___
        jtaylor___ commented
        Editing a comment
        Leading/trailing blanks shouldn't be an issue with %check() if using a varChar() or you do a %trim(). A MONITOR block is a good option too, especially if the exception is a rare occurrence.

      • JonBoy
        JonBoy commented
        Editing a comment
        Char or VarChar - if you include a blank in the check string then it won't catch errors where there is an embedded blank. That's why I used %Trim to remove the leading and trailing blanks.

    • #5
      If you are working with a display file and want to prevent anything but numbers from being entered into a field, then another alternative is to change the field to a data type of D which means digits only. This data type can be used on character or numeric fields which is determined by whether the decimal positions of the field is blank (character) or zero (numeric). If you need decimal positions greater than zero then this will not work. More information is in the DDS Reference Manual.
      Code:
      AAN01N02N03T.Name++++++RLen++TDpBLinPosFunctions+++++++++++++++++++++++++++
      A            CHAR          10D  B  8  7

      Comment


      • #6
        When you say you are restricted to fixed format (I feel your pain - I was in the same boat for the first 8.5 years of my career as an iSeries dev, 2008-2016), how restricted? Are you limited to opcodes of the factor 1, factor 2, result type, or can you use extended factor 2?
        If you can use extended factor 2, then you can use %check like this:
        Code:
             D NUMERIC         C                   const('1234567890')
             D myField         S             10A
        
             C                   If        %trim(myField) = *blanks
             C* Field is blank         
             C                   ElseIf    %check(NUMERIC:%trim(myField)) <> 0
             C* Field is not numeric
             C                   Else
             C* Field is ok
             C                   EndIf
        But personally I'm a fan of the MONITOR method as described by JonBoy

        Comment

        Working...
        X