Data Validation Postcode - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Data Validation Postcode

[code]procedure TForm2.Edit7Exit(Sender: TObject);
{upon the exit of this data input object, both the presense is checked
and the format of the postcode is checked or validated}
var
i:integer;
ok:boolean;
begin
ok := false;
i:=1;
if (edit7.text[i]='B') then {if first char is 'B'}
else if (edit7.text[i]='b') then {if first char is 'b'}
if (edit7.text[i+1]='T') then {if second char is 'T'}
else if (edit7.text[i+1]='t') then {if second char is 't'}
if (edit7.Text[i+2]in Digits) then
if (edit7.Text[i+3]in Digits) then {if next three are digits}
if (edit7.Text[i+4]in Digits) then
if (edit7.Text[i+5]in Letters) then
if (edit7.Text[i+6]in Letters) then {if last two are letters}
ok := true;

if ok=false then edit7.Color:= clLime
{if any of the above in incorrect then make the colour lime green to show error}
else if length(edit7.Text)=0 then edit7.Color:=clred
{or if there is nothing there, make it red}
else edit7.color:=clWhite;
{else it is white}[/code]

I want the first character to be either B or b without an error and the second char to be either a t or T without an error but with this code, it allows b but not B and t but not T.

HELP

Comments

  • zibadianzibadian Posts: 6,349Member
    : [code]procedure TForm2.Edit7Exit(Sender: TObject);
    : {upon the exit of this data input object, both the presense is checked
    : and the format of the postcode is checked or validated}
    : var
    : i:integer;
    : ok:boolean;
    : begin
    : ok := false;
    : i:=1;
    : if (edit7.text[i]='B') then {if first char is 'B'}
    : else if (edit7.text[i]='b') then {if first char is 'b'}
    : if (edit7.text[i+1]='T') then {if second char is 'T'}
    : else if (edit7.text[i+1]='t') then {if second char is 't'}
    : if (edit7.Text[i+2]in Digits) then
    : if (edit7.Text[i+3]in Digits) then {if next three are digits}
    : if (edit7.Text[i+4]in Digits) then
    : if (edit7.Text[i+5]in Letters) then
    : if (edit7.Text[i+6]in Letters) then {if last two are letters}
    : ok := true;
    :
    : if ok=false then edit7.Color:= clLime
    : {if any of the above in incorrect then make the colour lime green to show error}
    : else if length(edit7.Text)=0 then edit7.Color:=clred
    : {or if there is nothing there, make it red}
    : else edit7.color:=clWhite;
    : {else it is white}[/code]
    :
    : I want the first character to be either B or b without an error and the second char to be either a t or T without an error but with this code, it allows b but not B and t but not T.
    :
    : HELP
    :
    I would use Upcase() to force the letters to be uppercase in the comparison:
    [code]
    if Upcase(edit7.text[i])='B' then {if first char is 'B' or 'b'}
    [/code]
    You might also consider proving that text is invalid instead of valid. In most cases that's much simpler:
    [code]
    ok := true; // Assume Text is correct
    if Upcase(edit7.text[i])<>'B' then ok := false; // first char is not 'B' or 'b'
    if Upcase(edit7.text[i+1])<>'T' then ok := false; // second char is not 'T' or 't'
    if not (edit7.Text[i+2]in Digits) then ok := false; // etc
    // ....
    if ok=false then edit7.Color:= clLime
    {if any of the above in incorrect then make the colour lime green to show error}
    else if length(edit7.Text)=0 then edit7.Color:=clred
    {or if there is nothing there, make it red}
    else edit7.color:=clWhite;
    [/code]

  • sarkiemarkiesarkiemarkie Posts: 89Member
    : : [code]procedure TForm2.Edit7Exit(Sender: TObject);
    : : {upon the exit of this data input object, both the presense is checked
    : : and the format of the postcode is checked or validated}
    : : var
    : : i:integer;
    : : ok:boolean;
    : : begin
    : : ok := false;
    : : i:=1;
    : : if (edit7.text[i]='B') then {if first char is 'B'}
    : : else if (edit7.text[i]='b') then {if first char is 'b'}
    : : if (edit7.text[i+1]='T') then {if second char is 'T'}
    : : else if (edit7.text[i+1]='t') then {if second char is 't'}
    : : if (edit7.Text[i+2]in Digits) then
    : : if (edit7.Text[i+3]in Digits) then {if next three are digits}
    : : if (edit7.Text[i+4]in Digits) then
    : : if (edit7.Text[i+5]in Letters) then
    : : if (edit7.Text[i+6]in Letters) then {if last two are letters}
    : : ok := true;
    : :
    : : if ok=false then edit7.Color:= clLime
    : : {if any of the above in incorrect then make the colour lime green to show error}
    : : else if length(edit7.Text)=0 then edit7.Color:=clred
    : : {or if there is nothing there, make it red}
    : : else edit7.color:=clWhite;
    : : {else it is white}[/code]
    : :
    : : I want the first character to be either B or b without an error and the second char to be either a t or T without an error but with this code, it allows b but not B and t but not T.
    : :
    : : HELP
    : :
    : I would use Upcase() to force the letters to be uppercase in the comparison:
    : [code]
    : if Upcase(edit7.text[i])='B' then {if first char is 'B' or 'b'}
    : [/code]
    : You might also consider proving that text is invalid instead of valid. In most cases that's much simpler:
    : [code]
    : ok := true; // Assume Text is correct
    : if Upcase(edit7.text[i])<>'B' then ok := false; // first char is not 'B' or 'b'
    : if Upcase(edit7.text[i+1])<>'T' then ok := false; // second char is not 'T' or 't'
    : if not (edit7.Text[i+2]in Digits) then ok := false; // etc
    : // ....
    : if ok=false then edit7.Color:= clLime
    : {if any of the above in incorrect then make the colour lime green to show error}
    : else if length(edit7.Text)=0 then edit7.Color:=clred
    : {or if there is nothing there, make it red}
    : else edit7.color:=clWhite;
    : [/code]
    :
    :


    Excellent, Thank you!!
Sign In or Register to comment.