[b][red]This message was edited by hunny_bunny at 2004-4-25 2:11:26[/red][/b][hr]
In my program, the user enters a code which is 7 characters long. the first two characters have to be letters, the next two have to be numbers and the remaining three have to be letters. i want to ensure that the user can not save any incorrect entries of that code. i need to validate it but how would i do that? i need a message dialogue box to appear when the user clicks save and if the code is wrong to say something along the lines of 'you've entered the incorrect code'
Comments
: In my program, the user enters a code which is 7 characters long. the first two characters have to be letters, the next two have to be numbers and the remaining three have to be letters. i want to ensure that the user can not save any incorrect entries of that code. i need to validate it but how would i do that? i need a message dialogue box to appear when the user clicks save and if the code is wrong to say something along the lines of 'you've entered the incorrect code'
:
:
:
Hi,
i know zibadian is a guy who knows verry much about delphi, but he isn't the only one on this forum that can awnser you're question.
but u need an awnser wel here it is.
[code]function IsChar( Charecter: Char ): Boolean;
begin
Result := (ord(Charecter)>=ord('a')) and (ord(Charecter)<=ord('z'));
end;
function IsNumber( Charecter: Char ): Boolean;
begin
Result := (ord(Charecter)>=ord('0')) and (ord(Charecter)<=ord('9'));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if( IsChar(Edit1.Text[1]) and
IsChar(Edit1.Text[2]) and
IsNumber(Edit1.Text[3]) and
IsNumber(Edit1.Text[4]) and
IsChar(Edit1.Text[5]) and
IsChar(Edit1.Text[6]) and
IsChar(Edit1.Text[7]) )then
begin
// Do what u need to do here.
end
else MessageDlg( 'you''ve entered the incorrect code', mtError, [mbOK], 0 );
end;
[/code]
Greetz SRK.
: : In my program, the user enters a code which is 7 characters long. the first two characters have to be letters, the next two have to be numbers and the remaining three have to be letters. i want to ensure that the user can not save any incorrect entries of that code. i need to validate it but how would i do that? i need a message dialogue box to appear when the user clicks save and if the code is wrong to say something along the lines of 'you've entered the incorrect code'
: :
: :
: :
: Hi,
:
: i know zibadian is a guy who knows verry much about delphi, but he isn't the only one on this forum that can awnser you're question.
:
: but u need an awnser wel here it is.
: [code]function IsChar( Charecter: Char ): Boolean;
: begin
: Result := (ord(Charecter)>=ord('a')) and (ord(Charecter)<=ord('z'));
: end;
:
: function IsNumber( Charecter: Char ): Boolean;
: begin
: Result := (ord(Charecter)>=ord('0')) and (ord(Charecter)<=ord('9'));
: end;
:
: procedure TForm1.Button1Click(Sender: TObject);
: begin
: if( IsChar(Edit1.Text[1]) and
: IsChar(Edit1.Text[2]) and
: IsNumber(Edit1.Text[3]) and
: IsNumber(Edit1.Text[4]) and
: IsChar(Edit1.Text[5]) and
: IsChar(Edit1.Text[6]) and
: IsChar(Edit1.Text[7]) )then
: begin
: // Do what u need to do here.
: end
: else MessageDlg( 'you''ve entered the incorrect code', mtError, [mbOK], 0 );
: end;
: [/code]
:
: Greetz SRK.
:
Can also use a maskedit with a nice mask.
: : [b][red]This message was edited by hunny_bunny at 2004-4-25 2:11:26[/red][/b][hr]
: : In my program, the user enters a code which is 7 characters long. the first two characters have to be letters, the next two have to be numbers and the remaining three have to be letters. i want to ensure that the user can not save any incorrect entries of that code. i need to validate it but how would i do that? i need a message dialogue box to appear when the user clicks save and if the code is wrong to say something along the lines of 'you've entered the incorrect code'
: :
: :
: :
: Hi,
:
: i know zibadian is a guy who knows verry much about delphi, but he isn't the only one on this forum that can awnser you're question.
:
: but u need an awnser wel here it is.
: [code]function IsChar( Charecter: Char ): Boolean;
: begin
: Result := (ord(Charecter)>=ord('a')) and (ord(Charecter)<=ord('z'));
: end;
:
: function IsNumber( Charecter: Char ): Boolean;
: begin
: Result := (ord(Charecter)>=ord('0')) and (ord(Charecter)<=ord('9'));
: end;
:
: procedure TForm1.Button1Click(Sender: TObject);
: begin
: if( IsChar(Edit1.Text[1]) and
: IsChar(Edit1.Text[2]) and
: IsNumber(Edit1.Text[3]) and
: IsNumber(Edit1.Text[4]) and
: IsChar(Edit1.Text[5]) and
: IsChar(Edit1.Text[6]) and
: IsChar(Edit1.Text[7]) )then
: begin
: // Do what u need to do here.
: end
: else MessageDlg( 'you''ve entered the incorrect code', mtError, [mbOK], 0 );
: end;
: [/code]
:
: Greetz SRK.
:
[code]
function Matches(const Code: string): Integer;
type
TCharSet = set of Char;
const
cStringLength = 7;
cNumbers = ['0'..'9'];
cABC = ['A'..'Z', 'a'..'z'];
StrFormat : array[1..cStringLength] of TCharSet = (cNumbers, cNumbers, cABC, cABC, cNumbers, cNumbers, cNumbers);
begin
Result := Length(Code) + 1;
if Result > cStringLength + 1 then
Exit;
for Result := 1 to Length(Code) do
if not (Code[Result] in StrFormat[Result]) then
Exit;
Result := -1;
if Length(Code) <> cStringLength then
Exit;
Result := 0;
end;
procedure TfrmMain.btnMatchesClick(Sender: TObject);
var
Pos: Integer;
begin
Pos:= Matches(edInput.Text);
case Pos of
-1 : Application.MessageBox(Input is to short, should be 7', '', MB_OK + MB_ICONINFORMATION + MB_SETFOREGROUND + MB_TASKMODAL);
0: Application.MessageBox('Ok', '', MB_OK + MB_ICONINFORMATION + MB_SETFOREGROUND + MB_TASKMODAL);
else
Application.MessageBox(PChar(Format('Error at %d', [Pos])), '', MB_OK + MB_ICONINFORMATION + MB_SETFOREGROUND + MB_TASKMODAL);
end;
end;
[/code]
This one has more user feedback and shows the use of sets and const arrays.