Search code examples
validationrexx

Validation of data entry in REXX


How to enter only integers in REXX. The program I have manages to avoid some entries, but when the number contains a character such as a comma, percentage, cipher, among others, it causes the program to stop. How to validate these operations and let the program operate only with integer input.

do forever
  call charout, "Enter number: "
  pull NumInt
  say

  if (NumInt\== '') then 
    do
      if (datatype(value(NumInt), 'W')) then 
        leave
      else 
      do
        say "Error: Enter only whole number."
        say
      end
    end
  else
    do
      say "Error: <Enter> key accidentally triggered."
      say
    end
end

say "Valid input provided: " || NumeroInteiro

say
call charout, "Press <Enter> to finish... "
pull

exit

Solution

  • Based on the structure of your program, I believe you want something like:

    charSpecial: procedure
    arg String
    Char = "!@#$%^&*()-_=+[]{}|;:',<>/?`~" || '"'
    do i = 1 to length(Char)
      if pos(substr(Char, i, 1), String) > 0 then 
        return 1
    end
    return 0
    

    See the use of this in the code:

    do forever
      call charout, "Enter number: "
      pull NumInt
      say
    
      if (NumInt \== '') then 
        do
          if (charSpecial(NumInt)) then 
            do
              say "Error: Special character not allowed."
              say
            end
          else 
            if (datatype(value(NumInt), 'W')) then 
              leave
            else 
              do
                say "Error: Enter only whole number."
                say
              end
        end
      else
        do
          say "Error: <Enter> key accidentally triggered."
          say
        end
    end
    
    say "Valid input provided: " || NumInt
    
    say
    call charout, "Press <Enter> to finish... "
    pull
    
    exit