Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

delphi edit.text credtcard like, 25 characters

Posted on 2010-09-15
14
Medium Priority
?
824 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

636 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