Recent

Author Topic: Test Masking without Tmaskedit  (Read 1654 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Test Masking without Tmaskedit
« on: July 29, 2024, 04:32:58 pm »
Hi All

Thank you for all the help so far!
I dont like the Tmaskedit control with its little markers.
I know it took a bit of code back in VB, so it will probably be shorter in Pascal.

I need to limit a Tedit to the following mask LL#########LL
13 characters, TWO LETTERS, 9 NUMBERS, TWO letters. (Tracking numbers)

I truly appreciate all the help! I have made a lot of progress in a very short time.

Regards,

Peter

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: Test Masking without Tmaskedit
« Reply #1 on: July 29, 2024, 04:39:20 pm »
Try TRegExpr with the following pattern:
Code: Pascal  [Select][+][-]
  1. '^[A-Za-z]{2}\d{9}[A-Za-z]{2}$'

Example:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$endif}
  2. uses
  3.  RegExpr;
  4.  
  5. var
  6.   R:TRegExpr;
  7. begin
  8.   R:=TRegExpr.create('^[A-Za-z]{2}\d{9}[A-Za-z]{2}$');
  9.   try
  10.     writeln(R.Exec('LL123456789KK')); // true
  11.     writeln(R.Exec('L123456789KK'));  // false
  12.     writeln(R.Exec('GG123456789kk')); // true
  13.     writeln(R.Exec('LL123489KKKKK')); // false
  14.   finally
  15.     R.Free;
  16.   end;
  17.   readln;
  18. end.
You can subsequently validate a normal TEdit with the above expression code.
If only capitals are allowed the expression becomes:
Code: Pascal  [Select][+][-]
  1. '^[A-Z]{2}\d{9}[A-Z]{2}$'
  2.     writeln(R.Exec('GG123456789kk')); // would then return false


« Last Edit: July 29, 2024, 05:00:42 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: Test Masking without Tmaskedit
« Reply #2 on: July 29, 2024, 05:23:50 pm »
Something like
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, RegExpr;
  9.  
  10. type
  11.   { TForm1 }
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     Edit1: TEdit;
  15.     procedure Button1Exit(Sender: TObject);
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormDestroy(Sender: TObject);
  18.   private
  19.     R:TRegExpr;
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32. // also set TEdit.CharCase to ecUpperCase.
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35.   R:=TRegExpr.create('^[A-Z]{2}\d{9}[A-Z]{2}$');
  36. end;
  37.  
  38. procedure TForm1.Button1Exit(Sender: TObject);
  39. begin
  40.   If R.Exec(Edit1.Text) = true then
  41.     ShowMessage('This input is valid ')
  42.   else
  43.     ShowMessage('This input is not valid ')
  44. end;
  45.  
  46. procedure TForm1.FormDestroy(Sender: TObject);
  47. begin
  48.   R.Free;
  49. end;
  50.  
  51. end.
  Q&D...
« Last Edit: July 29, 2024, 05:40:11 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

jamie

  • Hero Member
  • *****
  • Posts: 6588
Re: Test Masking without Tmaskedit
« Reply #3 on: July 30, 2024, 12:07:04 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. begin
  4.   If Key in [VK_back,VK_LEFT,VK_RIGHT,VK_LSHIFT,VK_RSHIFT,VK_CAPITAL] THen exit;
  5.   If Sender is TEdit then with TEdit(Sender) do
  6.   Begin
  7.     if length(Caption) = 13 Then Begin Key := 0;Beep; Exit End;
  8.     If (selStart in [0..1,11..12]) Then
  9.        Begin
  10.         If Not (Key in [VK_A..VK_Z]) Then Begin beep; Key := 0; End;
  11.         Exit;
  12.        end;
  13.    if SelStart in [2..10] Then
  14.       If Not (Key in [VK_0..VK_9]) Then Begin Key := 0;Beep; End;
  15.   End;
  16. End;
  17.  
  18.  
  19.  

Include the "LclType" unit
The only true wisdom is knowing you know nothing

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Test Masking without Tmaskedit
« Reply #4 on: July 30, 2024, 11:35:28 am »
Thanks fellows.

The last example gives me a bundle of errors. (Still need to go see why)
The top two validates on exit. They work, but i am trying to force correct entry as we go.
(Like a masked edit would do)
Perhaps a simpler question, can we prevent the Maskedit from showing the lines ?

Thanks for all the support.

-Peter

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Test Masking without Tmaskedit
« Reply #5 on: July 30, 2024, 11:44:10 am »
Back in my Vb days i created a case statement on the keydown.

The maxlen on the control will handle the length limitation of the text.
I verified the length of the text as it is typed, then on :
characters 1&2 allow ALPHABET, characters 3-11 NUmbers and then 12 &13 again letters.

I haven't gotten to CASE statements & text lengths yet (learning fast, but it will be a while), so if you can perhaps point me in that direction i should get there!

Thanks mates.

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Test Masking without Tmaskedit
« Reply #6 on: July 30, 2024, 02:27:00 pm »
I came up with a rudimentary IF solution.
I am quite certain that it can be much improved in a CASE statement.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit5KeyPress(Sender: TObject; var Key: char);
  2.  
  3. begin
  4.   if length(edit5.Text) <= 1 then
  5.   begin
  6.    if not (Key in ['a'..'z', 'A'..'Z', '.', #8, #9]) then Key := #0;
  7.    end;
  8.   if (length(edit5.text) > 1) and (length(edit5.text) <= 10) then
  9.   begin
  10.    if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;
  11.   end;
  12.   if length(edit5.Text) >=11 then
  13.   begin
  14.    if not (Key in ['a'..'z', 'A'..'Z', '.', #8, #9]) then Key := #0;
  15.   end;
  16. end;      


I know this is not the best method. Some pointers on making it better would be much appreciated.

-Peter

Khrys

  • Jr. Member
  • **
  • Posts: 97
Re: Test Masking without Tmaskedit
« Reply #7 on: July 30, 2024, 03:56:45 pm »
I highly encourage every programmer to look into regular expressions; when used (in moderation!), regexes can be extremely useful for search, replace and validation operations (just don't use it for extended parsing).
Here's a pretty cool interactive website for testing regular expressions; I put in Thaddy's regex, which does exactly what you need.

jamie

  • Hero Member
  • *****
  • Posts: 6588
Re: Test Masking without Tmaskedit
« Reply #8 on: July 30, 2024, 10:40:35 pm »
I highly encourage every programmer to look into regular expressions; when used (in moderation!), regexes can be extremely useful for search, replace and validation operations (just don't use it for extended parsing).
Here's a pretty cool interactive website for testing regular expressions; I put in Thaddy's regex, which does exactly what you need.

Go ahead, you can have it.

 All the script kiddies will love you.

The only true wisdom is knowing you know nothing

Khrys

  • Jr. Member
  • **
  • Posts: 97
Re: Test Masking without Tmaskedit
« Reply #9 on: July 31, 2024, 06:46:42 am »
Go ahead, you can have it.

 All the script kiddies will love you.

That's why I wrote "in moderation:)
Regex syntax is terribly terse and opaque to those who haven't joined the cult, after all...

Zvoni

  • Hero Member
  • *****
  • Posts: 2692
Re: Test Masking without Tmaskedit
« Reply #10 on: July 31, 2024, 07:24:48 am »
Back in my Vb days i created a case statement on the keydown.

The maxlen on the control will handle the length limitation of the text.
I verified the length of the text as it is typed, then on :
characters 1&2 allow ALPHABET, characters 3-11 NUmbers and then 12 &13 again letters.

I haven't gotten to CASE statements & text lengths yet (learning fast, but it will be a while), so if you can perhaps point me in that direction i should get there!

Thanks mates.
It‘s possible with regex to evaluate on the fly during entering text, but you have to build the pattern dynamically, keeping an eye on the position and total length
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Joanna

  • Hero Member
  • *****
  • Posts: 1101
Re: Test Masking without Tmaskedit
« Reply #11 on: July 31, 2024, 01:24:27 pm »
Hi All

Thank you for all the help so far!
I dont like the Tmaskedit control with its little markers.
I know it took a bit of code back in VB, so it will probably be shorter in Pascal.

I need to limit a Tedit to the following mask LL#########LL
13 characters, TWO LETTERS, 9 NUMBERS, TWO letters. (Tracking numbers)

I truly appreciate all the help! I have made a lot of progress in a very short time.

Regards,

Peter
I use mask edit but I never noticed any markers. What do they look Like?
« Last Edit: July 31, 2024, 11:00:42 pm by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: Test Masking without Tmaskedit
« Reply #12 on: August 04, 2024, 08:54:25 am »
All the script kiddies will love you.
It is not script, but compiled to specialized p-code.
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: Test Masking without Tmaskedit
« Reply #13 on: August 04, 2024, 09:06:27 am »
I am quite certain that it can be much improved in a CASE statement.
Like so?
Code: Pascal  [Select][+][-]
  1.   case length(Edit5.Text) of
  2.   1..2,11..12:if not (Key in [#8, #9,'A'..'Z','a'..'z' ]) then Key := #0;
  3.   3..10:if not (Key in [#8,#9,'.', '0'..'9']) then Key := #0;
  4.   else
  5.     Key := #0;
  6.   end;
« Last Edit: August 04, 2024, 09:15:23 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Zvoni

  • Hero Member
  • *****
  • Posts: 2692
Re: Test Masking without Tmaskedit
« Reply #14 on: August 05, 2024, 08:26:10 am »
And since TS is using KeyPress (Down/Up?)-Event, i'm going to throw a spanner into it by using Copy/Paste to insert an illegal entry into the TextBox....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018