Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14007

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Validation program Posted by stevensingh on 10 Sept 2002 at 5:45 AM
I am am writing a program that validates certain user inputs in a wages program. One of the inputs is a taxcode - this comprises of 3 digits and 1 letter ('L' or 'H').

How can I validate the user input taxcodes so that only taxcodes that consist of 3 digits and 1 letter (l or H) are accepted?


Report
Re: Validation program Posted by tron on 10 Sept 2002 at 7:36 AM
: I am am writing a program that validates certain user inputs in a wages program. One of the inputs is a taxcode - this comprises of 3 digits and 1 letter ('L' or 'H').
:
: How can I validate the user input taxcodes so that only taxcodes that consist of 3 digits and 1 letter (l or H) are accepted?
:
:
:

You might want to modify this to fit your needs:

function inputTaxCode : String;
var s   : String;
      ch : Char;
begin
  s := '';
  while (true) do begin

    {wait until keypressed}
    repeat until (keypressed);
    {get the key}
    ch := readKey;

    {only process ASCII keys}
    if NOT(ch = 0) then begin
      ch := UPCASE(ch);

     {return}
      if (ch = #13) AND (length(s) = 4) then begin
        inputTaxCode := s;
        exit;

     {backspace}
      end else if (ch = #8) AND NOT(length(s) = 0) then begin
        s := copy(s, 1,length(s)-1);
        write(#8);

      {digits and L and H}
      end else  if ((ch >= '0') AND (ch <= '9') AND (length(s) < 3)) OR
          (((ch = 'L') OR (ch = 'H')) AND  (length(s) = 3)) then begin
         s := s + ch;
         write(ch);
       end;

    end else ch := readKey; {ignore the extended keycodes}
  end;
end;


There are sure some syntax errors in the code above - I didn't check it with pascal.

tron.
Report
Re: Validation program Posted by weedsmoka on 11 Sept 2002 at 3:33 AM
: I am am writing a program that validates certain user inputs in a wages program. One of the inputs is a taxcode - this comprises of 3 digits and 1 letter ('L' or 'H').
:
: How can I validate the user input taxcodes so that only taxcodes that consist of 3 digits and 1 letter (l or H) are accepted?
:
:
:


my version:
{Returns TRUE if ch is a number}
function IsDigit(ch:char):boolean;
begin
     IsDigit := (ch in ['0'..'9']);
end;

{Returns TRUE if ch is either 'H' or 'L'}
function IsHL (ch:char):boolean;
begin
     IsHL := ((UpCase(ch) = 'H') or (UpCase(ch) = 'L'));
end;


{Returns TRUE if tax code is valid}
function TaxCodeValid(s:string):boolean;
var
   result:boolean;
   i:byte;
begin
     result:=true;

     {Remove any spaces from the beginning of the string}
     while (s[1] = #32) and (byte(s[0]) > 0) do delete(s,1,1);

     {Remove any spaces from the end of the string}
     while (s[byte(s[0])] = #32) and (byte(s[0]) > 0) do dec(byte(s[0]));

     if byte(s[0]) <> 4 then result:=false
     else
     begin
          for i:=1 to 3 do
          if (not IsDigit(s[i])) then result:=false;
          if (not IsHL (s[4])) then result:=false;
     end;
     TaxCodeValid:=result;

end;

begin
     writeln(TaxCodeValid('123H'));
     writeln(TaxCodeValid('1234'));
end.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.