• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 838
  • Last Modified:

delphi edit.text credtcard like, 25 characters

Hi

I want a user to type 5 charcters into edit1.text but on typing the 6th charcater it them puts a - after the 5th then the user can keep on typing more, then again seperated by a - after the 10th on typing the 11th

in simple terms, i want a user to type out a 25 digit of mixed letters /numbers and seperated by - after every 5 of them.

example.

GFH7G-567GF-GHBNG-JUH8G-HGJBM



The user doesnt type -.


Ive tried setfocus and different ways that im not too happy about.

500 points, need this answerd asap ok.

thankyou
0
Emailcoder1
Asked:
Emailcoder1
  • 7
  • 4
  • 3
1 Solution
 
8080_DiverCommented:
You are probably going to have to have an On Key Press event handler that works this magic.
Essentially, based on the length of the TEdit.text , when a key is hit and the key is letter or digit, then it needs to set the text to match your formatting requirements.  (You can also force the alpha characters to uppercase at that point.)
0
 
Emailcoder1Author Commented:
Thanks for your answer. I want to do this the simpliest way.

Can you provide the code for this, or if possible.. if its going to be very hard, maybe just have the textbox just include - - - - already so every 5 characters its -
0
 
Emailcoder1Author Commented:
Ah sorry i forgot to add, at no stage can the user press a key, it must be all based on typing out a 25 digit number, you know, like in seriels for programs, its all "in one motion" so to speak.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
8080_DiverCommented:
Try the following snippet:
procedure TfrmTWGRoadWarriorMain.Edit1KeyPress(Sender: TObject;
  var Key: Char);
begin
    case Key of
      '0'..'9','A'..'Z','a'..'z'
            : begin
                case Length(Edit1.Text) of
                   5,11,17,23  : begin {}
                          Edit1.Text     :=  Edit1.Text + '-';
                        end;
                  else ;
                end; {case}
                Edit1.SelStart := Length(Edit1.Text);
              end;
      else ;
    end; {case boolean}
end;

Open in new window

0
 
epasquierCommented:
> at no stage can the user press a key, it must be all based on typing out a 25 digit number,
> you know, like in seriels for programs, its all "in one motion" so to speak.

I don't understand your remarks with NO KEY PRESSED ?
In one motion ??? what does that mean ?
0
 
8080_DiverCommented:
at no stage can the user press a key,
Uh . . . so, how does the user type out the 25 digit number wuithout pressing a key? ;-)
0
 
Emailcoder1Author Commented:
sorry guys lol. i thought you were stating that a user must press a key after hes typed it out, like a return key ect.. ah its ok.

Ill try that snippet now, thanks.
0
 
Emailcoder1Author Commented:
it states, edit1keypress is undeclared.
0
 
8080_DiverCommented:
DUH!  You have to go to the Edit1 object and cause the KeyPress event to have an event handler.  Just double click on the On Key Press event handler field.
0
 
Emailcoder1Author Commented:
i already did that,but when i redit it again, it worked. I did it so fast it was on keydown. Ok thanks for your help with that, i learned alot. cheers
0
 
epasquierCommented:
Here it is, it will remove the '-' when needed if you hit backspace
procedure TForm1.edtRegKeyKeyPress(Sender: TObject; var Key: Char);
Var
 edt:TEdit;
 Str:String;
 L:Integer;
begin
 if Not (Key In ['0'..'9',#8]) Then
  begin
   Key:=#0;
   Exit;
  end;
 edt:=Sender As TEdit;
 Str:=edt.Text;
 L:=Length(Str);
 if Key=#8 Then
  begin
   if (L>0) And (Str[L]='-') Then Edt.Text:=Copy(Str,1,L-1);
  end Else
   if (((L+1) Mod 6)=0)
  // And (L<edt.MaxLength) // check that if MaxLength is correctly set (including the -)
    Then Edt.Text:=Str+'-';
 edt.SelStart:=L+1;
end;

Open in new window

0
 
epasquierCommented:
Oh, I forgot to tell you, for this code to work perfectly, you need to set the MaxLength property of the edit to the maximum number of chars it will contain = 25+4 = 29
That will manage the limit cases

And if that is done, you can uncomment the 20th line (I commented it because if you let MaxLength to its 0 default value it does not work at all)

While I'm at it, with the code below I added literals and it filters lower case characters
procedure TForm1.edtRegKeyKeyPress(Sender: TObject; var Key: Char);
Var
 edt:TEdit;
 Str:String;
 L:Integer;
Const
 Allowed=[#8,'0'..'9','A'..'Z']; // Add or remove literals as you need
begin
 Key:=UpCase(Key);
 if Not (Key In Allowed) Then
  begin
   Key:=#0;
   Exit;
  end;
 edt:=Sender As TEdit;
 Str:=edt.Text;
 L:=Length(Str);
 if Key=#8 Then
  begin
   if (L>0) And (Str[L]='-') Then Edt.Text:=Copy(Str,1,L-1);
  end Else
   if (((L+1) Mod 6)=0) And (L<edt.MaxLength) // check that if MaxLength is correctly set (including the -)
    Then Edt.Text:=Str+'-';
 edt.SelStart:=L+1;
end;

Open in new window

0
 
Emailcoder1Author Commented:
Thats fine, i alredy changed all the proporties to what i need, thanks again for your help.
0
 
Emailcoder1Author Commented:
A+
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now