## Pascal

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

This Forum Only

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?

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.
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.
```