Solved

delphi edit.text credtcard like, 25 characters

Posted on 2010-09-15
14
784 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
  • 7
  • 4
  • 3
14 Comments
 
LVL 22

Expert Comment

by:8080_Diver
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:8080_Diver
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Emailcoder1
Comment Utility
it states, edit1keypress is undeclared.
0
 
LVL 22

Expert Comment

by:8080_Diver
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thats fine, i alredy changed all the proporties to what i need, thanks again for your help.
0
 

Author Closing Comment

by:Emailcoder1
Comment Utility
A+
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now