Mask/Format text during typing

I have an edit box. While the user is entering numbers, I want dashes inserted at certain points, and a limit on how many numbers can be entered.

IE, I want it to end up like this:

9822-00-234-2345


So the dashes are added while the user is typing the number, and only 13 digits can be entered. They also need to be able to delete, and the dashes must remain in the correct places.


I'm totally lost on this. Any tips?
jlinkeAsked:
Who is Participating?
 
pcsentinelConnect With a Mentor Commented:
Why not just use a masked edit control?

or you can use this

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
      if Length(Edit1.Text) in [4,8] then
  begin
        Edit1.Text:=Edit1.Text+'-';
    Edit1.SelStart:=Length(Edit1.Text);
  end;
end;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=VK_BACK) and (Copy(Edit1.Text,Length(Edit1.Text),1)='-') then
  begin
        Edit1.Text:=Copy(Edit1.Text,1,pred(Length(Edit1.Text)));
    Edit1.SelStart:=Length(Edit1.Text);
  end

end;


regards
0
 
pcsentinelCommented:
Sorry you need to add 12 to your set i.e.

if Length(Edit1.Text) in [4,8,12] then

regards
0
 
mikelittlewoodCommented:
He will also need to make sure the length does not go over 16 ... 13 digits plus 3 '-' signs

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Length(Edit1.Text) > 16 then
    Key := 0;
end;

or something like that
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
pcsentinelCommented:
Thanks mike forgot about the last bit

0
 
DavidBirch2dotComCommented:
Surely you would also need to limit which keys may be pressed?

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if not (Key in [#8, '0'..'9',#13]) then // you may also want to add the arrow keys
   begin
    beep;
    Result := #0;
   end;
end;
0
 
mikelittlewoodCommented:
Another good point David, forgot he only wanted numbers as well.
0
 
geobulCommented:
Hi,

Try TMaskEdit component with the following EditMask property value '!0000-00-000-0000;1; '.

Regards, Geo
0
 
pcsentinelCommented:
Geo, the only thing with maskedit is that the dashes are in there permanantly not integrated during typing

0
 
VoodoomanCommented:

Watching

How bad are masked edit controls............hang your head in shame Bill Gates.......

Masked Edit was introuduced in VB3/Win95, I never saw a good one......


Voodooman
0
 
geobulCommented:
pcsentinel, have you tried yours at all? Just try to delete using Backspace before '-' char.

I think I understood the requrements. Here is my version (not perfect though):

TForm1 = class(TForm)
    Edit1: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    bkp: boolean;
    del: boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_BACK then bkp := true
  else bkp := false;
  if Key = VK_DELETE then del := true
  else del := false;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
  s: string;
  i,k: integer;
begin
  k := Edit1.SelStart;
  s := StringReplace(Edit1.Text, '-', '', [rfReplaceAll]);
  i := Length(s);
  if i > 13 then begin
    i := 13;
    s := Copy(s,1,13);
  end;
  if i > 4 then begin
    s := Copy(s,1,4)+'-'+Copy(s,5,i);
    i := i + 1;
  end;
  if i > 7 then begin
    s := Copy(s,1,7)+'-'+Copy(s,8,i);
    i := i + 1;
  end;
  if i > 11 then begin
    s := Copy(s,1,11)+'-'+Copy(s,12,i);
  end;
  Edit1.Text := s;
  if (not bkp) then
    case k of
      4,7,11: k := k + 1;
      5,8,12: if not del then k := k + 1;
    end;
  Edit1.SelStart := k;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  bkp := false;
  del := false;
  SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);
end;

Regards, Geo
0
 
pcsentinelCommented:
Modified version of mine that should work

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
       if (not (Key in [VK_BACK, 0,9,VK_RETURN])) or (Length(Edit1.Text) > 15) then // you may also want to add the arrow keys
  begin
    beep;
    Key := 0;
  end;
  if (Key in [0,9]) and (Length(Edit1.Text) in [4,8,12]) then
  begin
    Edit1.Text:=Edit1.Text+'-';
    Edit1.SelStart:=Length(Edit1.Text);
  end;
end;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=VK_BACK) and (Copy(Edit1.Text,Length(Edit1.Text),1)='-') then
  begin
    Key:=0;
    Edit1.Text:=Copy(Edit1.Text,1,pred(Length(Edit1.Text)));
    Edit1.SelStart:=Length(Edit1.Text);
  end
end;
0
 
pcsentinelCommented:
Sorry I have to admit got it wrong again, problem with allowing chars input

The following seems to work for eveything except for the delete key

procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
  if ((not(Pos('-',Edit1.Text)<>0)) or (Edit3.SelStart>0)) and (ord(Key)=45) then
        key:=Chr(0);
{  if (Pos('.',Text)>0) and not(Pos('.',Edit3.SelText)<>0) and (ord(Key)=46) then
        key:=Chr(0);
}  if not (ord(key) in [8,13,48..57]) then
    key:=Chr(0);
  if (Length(Edit3.Text) in [4,8,12]) and (not (Ord(Key)=VK_BACK)) then
  begin
    Edit3.Text:=Edit3.Text+'-';
    Edit3.SelStart:=Length(Edit3.Text);
  end;
end;

procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if (Key=VK_BACK) and (Copy(Edit3.Text,Length(Edit3.Text),1)='-') then
  begin
    Key:=0;
    Edit3.Text:=Copy(Edit3.Text,1,pred(Length(Edit3.Text)));
    Edit3.SelStart:=Length(Edit3.Text);
  end
end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.