Solved

delphi edit.text credtcard like, 25 characters

Posted on 2010-09-15
14
792 Views
Last Modified: 2012-05-10
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
Comment
Question by:Emailcoder1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33683052
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
 

Author Comment

by:Emailcoder1
ID: 33683082
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
 

Author Comment

by:Emailcoder1
ID: 33683096
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:8080_Diver
ID: 33683235
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
 
LVL 25

Expert Comment

by:epasquier
ID: 33683240
> 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
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33683250
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
 

Author Comment

by:Emailcoder1
ID: 33683292
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
 

Author Comment

by:Emailcoder1
ID: 33683311
it states, edit1keypress is undeclared.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33683347
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
 

Author Comment

by:Emailcoder1
ID: 33683389
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
 
LVL 25

Expert Comment

by:epasquier
ID: 33683457
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
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33683582
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
 

Author Comment

by:Emailcoder1
ID: 33683722
Thats fine, i alredy changed all the proporties to what i need, thanks again for your help.
0
 

Author Closing Comment

by:Emailcoder1
ID: 33683725
A+
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question