Howdy, Stranger!

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

Categories

Check if character is in range ['A'..'Z']

mac_doggiemac_doggie Member Posts: 488
Hi,

Can anyone tell me why this code doesn't compile ? I get: Incompatible types. the copy function seems to be of type caption while ord only accepts a char. How could I solve this?

[code]
if( (ord(copy(edtPostcode.text,6,1)) < 65 ) then
begin
{...MyCode...}
end;
[/code]

I mean to build a check to see if character 6 and 7 are in the range ['A'..'Z'] I also tried:
[code]
if( copy(edtPostcode.text,6,1) in ['A'..'Z'] ) then
begin
{...MyCode...}
end;
[/code]

but that also didn't work...

Hope someone can help me.
;-)
-mac-
mailto:mac_doggie@hotmail.com
the Netherlands...

Comments

  • MasterijnMasterijn Member Posts: 188
    : Hi,
    :
    : Can anyone tell me why this code doesn't compile ? I get: Incompatible types. the copy function seems to be of type caption while ord only accepts a char. How could I solve this?
    :
    : [code]
    : if( (ord(copy(edtPostcode.text,6,1)) < 65 ) then
    : begin
    : {...MyCode...}
    : end;
    : [/code]
    :
    : I mean to build a check to see if character 6 and 7 are in the range ['A'..'Z'] I also tried:
    : [code]
    : if( copy(edtPostcode.text,6,1) in ['A'..'Z'] ) then
    : begin
    : {...MyCode...}
    : end;
    : [/code]
    :
    : but that also didn't work...
    :
    : Hope someone can help me.
    : ;-)
    : -mac-
    : mailto:mac_doggie@hotmail.com
    : the Netherlands...
    :
    :
    copy returns a (sub)string. To do a in set operation, it should be a char which indeed is a ordinal value. You can write it like this:
    [code]
    if Length(edtPostcode.text >= 7) then
    begin
    if (edtPostcode.text[6] in ['A'..'Z']) and (edtPostcode.text[7] in ['A'..'Z']) then
    // is a dutch postal code
    end
    [/code]
    But let's make it more beautifull:
    [code]
    function IsDutchPostalCode(const Code: string): Boolean;
    begin
    Result := False;
    if Length(Code) <> 7 then
    Exit;
    Result := ((Code[1] in ['0'..'9'])
    and (Code[2] in ['0'..'9'])
    and (Code[3] in ['0'..'9'])
    and (Code[4] in ['0'..'9'])
    and (Code[5] = ' ')
    and (Code[6] in ['A'..'Z'])
    and (Code[7] in ['A'..'Z']));
    end;
    [/code]
    And just for fun:
    [code]
    function IsDutchPostalCode(const Code: string): Boolean;
    begin
    Result := MatchesMask(Code, '[0-9][0-9][0-9][0-9] [A-Z][A-Z]');
    end;
    [/code]







  • mac_doggiemac_doggie Member Posts: 488
    Ok, thanks.

    Now how about this: I also have a field Huisnummer wich represent the housenumber. It can only be an integer number.

    [code]
    procedure TfrmMUTAdres.edtHuisnummerKeyPress(Sender: TObject;
    var Key: Char);
    begin
    if (not (Key in ['0'..'9'])) then Key := ' ';
    end;
    [/code]

    This reseults in a space being printed in the editbox. I can's make Key:=''; because that would not be a valid char, but a string. Is there a nice way to do this ??








    : : Hi,
    : :
    : : Can anyone tell me why this code doesn't compile ? I get: Incompatible types. the copy function seems to be of type caption while ord only accepts a char. How could I solve this?
    : :
    : : [code]
    : : if( (ord(copy(edtPostcode.text,6,1)) < 65 ) then
    : : begin
    : : {...MyCode...}
    : : end;
    : : [/code]
    : :
    : : I mean to build a check to see if character 6 and 7 are in the range ['A'..'Z'] I also tried:
    : : [code]
    : : if( copy(edtPostcode.text,6,1) in ['A'..'Z'] ) then
    : : begin
    : : {...MyCode...}
    : : end;
    : : [/code]
    : :
    : : but that also didn't work...
    : :
    : : Hope someone can help me.
    : : ;-)
    : : -mac-
    : : mailto:mac_doggie@hotmail.com
    : : the Netherlands...
    : :
    : :
    : copy returns a (sub)string. To do a in set operation, it should be a char which indeed is a ordinal value. You can write it like this:
    : [code]
    : if Length(edtPostcode.text >= 7) then
    : begin
    : if (edtPostcode.text[6] in ['A'..'Z']) and (edtPostcode.text[7] in ['A'..'Z']) then
    : // is a dutch postal code
    : end
    : [/code]
    : But let's make it more beautifull:
    : [code]
    : function IsDutchPostalCode(const Code: string): Boolean;
    : begin
    : Result := False;
    : if Length(Code) <> 7 then
    : Exit;
    : Result := ((Code[1] in ['0'..'9'])
    : and (Code[2] in ['0'..'9'])
    : and (Code[3] in ['0'..'9'])
    : and (Code[4] in ['0'..'9'])
    : and (Code[5] = ' ')
    : and (Code[6] in ['A'..'Z'])
    : and (Code[7] in ['A'..'Z']));
    : end;
    : [/code]
    : And just for fun:
    : [code]
    : function IsDutchPostalCode(const Code: string): Boolean;
    : begin
    : Result := MatchesMask(Code, '[0-9][0-9][0-9][0-9] [A-Z][A-Z]');
    : end;
    : [/code]
    :
    :
    :
    :
    :
    :
    :
    :
    ;-)
    -mac-
    mailto:mac_doggie@hotmail.com
    the Netherlands...


  • zibadianzibadian Member Posts: 6,349
    : Ok, thanks.
    :
    : Now how about this: I also have a field Huisnummer wich represent the housenumber. It can only be an integer number.
    :
    : [code]
    : procedure TfrmMUTAdres.edtHuisnummerKeyPress(Sender: TObject;
    : var Key: Char);
    : begin
    : if (not (Key in ['0'..'9'])) then Key := ' ';
    : end;
    : [/code]
    :
    : This reseults in a space being printed in the editbox. I can's make Key:=''; because that would not be a valid char, but a string. Is there a nice way to do this ??
    :
    :
    :
    :
    :
    :
    :
    :
    : : : Hi,
    : : :
    : : : Can anyone tell me why this code doesn't compile ? I get: Incompatible types. the copy function seems to be of type caption while ord only accepts a char. How could I solve this?
    : : :
    : : : [code]
    : : : if( (ord(copy(edtPostcode.text,6,1)) < 65 ) then
    : : : begin
    : : : {...MyCode...}
    : : : end;
    : : : [/code]
    : : :
    : : : I mean to build a check to see if character 6 and 7 are in the range ['A'..'Z'] I also tried:
    : : : [code]
    : : : if( copy(edtPostcode.text,6,1) in ['A'..'Z'] ) then
    : : : begin
    : : : {...MyCode...}
    : : : end;
    : : : [/code]
    : : :
    : : : but that also didn't work...
    : : :
    : : : Hope someone can help me.
    : : : ;-)
    : : : -mac-
    : : : mailto:mac_doggie@hotmail.com
    : : : the Netherlands...
    : : :
    : : :
    : : copy returns a (sub)string. To do a in set operation, it should be a char which indeed is a ordinal value. You can write it like this:
    : : [code]
    : : if Length(edtPostcode.text >= 7) then
    : : begin
    : : if (edtPostcode.text[6] in ['A'..'Z']) and (edtPostcode.text[7] in ['A'..'Z']) then
    : : // is a dutch postal code
    : : end
    : : [/code]
    : : But let's make it more beautifull:
    : : [code]
    : : function IsDutchPostalCode(const Code: string): Boolean;
    : : begin
    : : Result := False;
    : : if Length(Code) <> 7 then
    : : Exit;
    : : Result := ((Code[1] in ['0'..'9'])
    : : and (Code[2] in ['0'..'9'])
    : : and (Code[3] in ['0'..'9'])
    : : and (Code[4] in ['0'..'9'])
    : : and (Code[5] = ' ')
    : : and (Code[6] in ['A'..'Z'])
    : : and (Code[7] in ['A'..'Z']));
    : : end;
    : : [/code]
    : : And just for fun:
    : : [code]
    : : function IsDutchPostalCode(const Code: string): Boolean;
    : : begin
    : : Result := MatchesMask(Code, '[0-9][0-9][0-9][0-9] [A-Z][A-Z]');
    : : end;
    : : [/code]
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : ;-)
    : -mac-
    : mailto:mac_doggie@hotmail.com
    : the Netherlands...
    :
    :
    :
    If the key isn't valid, then you can set it to #0 to "remove" it from the key-press, as shown here:
    : [code]
    : procedure TfrmMUTAdres.edtHuisnummerKeyPress(Sender: TObject;
    : var Key: Char);
    : begin
    : if (not (Key in ['0'..'9'])) then Key := #0;
    : end;
    : [/code]

  • MasterijnMasterijn Member Posts: 188
    : : Ok, thanks.
    : :
    : : Now how about this: I also have a field Huisnummer wich represent the housenumber. It can only be an integer number.
    : :
    : : [code]
    : : procedure TfrmMUTAdres.edtHuisnummerKeyPress(Sender: TObject;
    : : var Key: Char);
    : : begin
    : : if (not (Key in ['0'..'9'])) then Key := ' ';
    : : end;
    : : [/code]
    : :
    : : This reseults in a space being printed in the editbox. I can's make Key:=''; because that would not be a valid char, but a string. Is there a nice way to do this ??
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : : : : Hi,
    : : : :
    : : : : Can anyone tell me why this code doesn't compile ? I get: Incompatible types. the copy function seems to be of type caption while ord only accepts a char. How could I solve this?
    : : : :
    : : : : [code]
    : : : : if( (ord(copy(edtPostcode.text,6,1)) < 65 ) then
    : : : : begin
    : : : : {...MyCode...}
    : : : : end;
    : : : : [/code]
    : : : :
    : : : : I mean to build a check to see if character 6 and 7 are in the range ['A'..'Z'] I also tried:
    : : : : [code]
    : : : : if( copy(edtPostcode.text,6,1) in ['A'..'Z'] ) then
    : : : : begin
    : : : : {...MyCode...}
    : : : : end;
    : : : : [/code]
    : : : :
    : : : : but that also didn't work...
    : : : :
    : : : : Hope someone can help me.
    : : : : ;-)
    : : : : -mac-
    : : : : mailto:mac_doggie@hotmail.com
    : : : : the Netherlands...
    : : : :
    : : : :
    : : : copy returns a (sub)string. To do a in set operation, it should be a char which indeed is a ordinal value. You can write it like this:
    : : : [code]
    : : : if Length(edtPostcode.text >= 7) then
    : : : begin
    : : : if (edtPostcode.text[6] in ['A'..'Z']) and (edtPostcode.text[7] in ['A'..'Z']) then
    : : : // is a dutch postal code
    : : : end
    : : : [/code]
    : : : But let's make it more beautifull:
    : : : [code]
    : : : function IsDutchPostalCode(const Code: string): Boolean;
    : : : begin
    : : : Result := False;
    : : : if Length(Code) <> 7 then
    : : : Exit;
    : : : Result := ((Code[1] in ['0'..'9'])
    : : : and (Code[2] in ['0'..'9'])
    : : : and (Code[3] in ['0'..'9'])
    : : : and (Code[4] in ['0'..'9'])
    : : : and (Code[5] = ' ')
    : : : and (Code[6] in ['A'..'Z'])
    : : : and (Code[7] in ['A'..'Z']));
    : : : end;
    : : : [/code]
    : : : And just for fun:
    : : : [code]
    : : : function IsDutchPostalCode(const Code: string): Boolean;
    : : : begin
    : : : Result := MatchesMask(Code, '[0-9][0-9][0-9][0-9] [A-Z][A-Z]');
    : : : end;
    : : : [/code]
    : : :
    : : :
    : : :
    : : :
    : : :
    : : :
    : : :
    : : :
    : : ;-)
    : : -mac-
    : : mailto:mac_doggie@hotmail.com
    : : the Netherlands...
    : :
    : :
    : :
    : If the key isn't valid, then you can set it to #0 to "remove" it from the key-press, as shown here:
    : : [code]
    : : procedure TfrmMUTAdres.edtHuisnummerKeyPress(Sender: TObject;
    : : var Key: Char);
    : : begin
    : : if (not (Key in ['0'..'9'])) then Key := #0;
    : : end;
    : : [/code]
    :
    :
    You can also use a MaskedEdit control. It also allows some formatting.
    B.T.W. in the Netherlands people can live on 15a or 18d, ect.. If you really dive into it, addresses are quite complex.


Sign In or Register to comment.