检查编辑框是否使用此格式

时间:2013-02-12 07:48:52

标签: delphi

我想查看并查看TEdit.text是否采用此格式123/45/678输入文字时 因此### / ## / ###

任何简单的方法吗? 感谢

3 个答案:

答案 0 :(得分:2)

假设你的面具非常简单,只有#和/它很容易编写测试函数:

function MatchesMask(const Text, Mask: string): Boolean;
var
  i: Integer;
begin
  Result := False;

  if Length(Text)<>Length(Mask) then
    exit;

  for i := 1 to Length(Text) do
    case Mask[i] of
    '#':
      if (Text[i]<'0') or (Text[i]>'9') then
        exit;
    else
      if Text[i]<>Mask[i] then
        exit;
    end;

  Result := True;
end;

答案 1 :(得分:2)

Function CheckStringWithMask(const Str,Mask:String):Boolean;
var
 i:Integer;
begin
  Result := true;
  if length(str)=length(Mask) then
    begin
    i := 0;
    While Result and  (I < Length(Str)) do
      begin
      inc(i);
      Result := Result and (Str[i] <> '#')
                and ((Mask[i] ='#') and (CharInSet(Str[i],['0'..'9']))
                or (Str[i]=Mask[i]));
      end;
    end
  else Result := false;
end;

答案 2 :(得分:2)

@David Heffernan's suggestion的变体:

function MatchesMask(const Text, Mask: string): Boolean;
var
  i: Integer;
begin
  Result := (Length(Text) = Length(Mask));

  if not Result then Exit;

  i := 0;
  while Result and (i < Length(Text)) do begin
    Inc(i);
    case Mask[i] of
    '#':
       Result := (Text[i] >= '0') and (Text[i] <= '9');
    else
       Result := (Text[i] = Mask[i]);
    end;
  end;    
end;