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