加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

验证 – Delphi设置编辑掩码,没有固定长度的字符

发布时间:2020-12-15 09:28:57 所属栏目:大数据 来源:网络整理
导读:我有一个TValueListEditor类型的对象,它包含Key列中某个函数的某些参数,以及一个用于测试该函数的相应值列的输入.我已根据参数的数据类型向值输入添加了编辑掩码.例如,参数Num1的类型为int,因此输入必须只是数字,但由于我事先并不知道确切的位数,有没有办法
我有一个TValueListEditor类型的对象,它包含Key列中某个函数的某些参数,以及一个用于测试该函数的相应值列的输入.我已根据参数的数据类型向值输入添加了编辑掩码.例如,参数Num1的类型为int,因此输入必须只是数字,但由于我事先并不知道确切的位数,有没有办法指定EditMask而没有固定长度的字符?

如果你看下面的代码,如果我需要一个float类型的值,我必须有一个点,但我不希望该点在那个确切的位置预定义.

if parser.sParams.Values[parser.sParams.Names[i]]='float' then
    begin
    lstValParamValues.ItemProps[parser.sParams.Names[i]].EditMask:='#########.#';
    end

也许我应该在EditMask上实现类似regex的东西?或者是否有另一种方法来实现值输入的验证?

解决方法

TItemProp.EditMask documentation:

Validation using the EditMask property is performed on a character-by-character basis.

所以你只能使用固定宽度的面具.这意味着您必须指定小数点的位置,以及要接受的前导和尾随数字的数量.

请考虑使用TValueListEditor.OnValidate事件:

Occurs when focus shifts away from a cell in the value list editor.

Write an OnValidate event handler to validate any edits the user enters in a cell before focus leaves it. OnValidate gives applications an opportunity to provide more validation than the EditMask property of the corresponding TItemProp object can supply.

OnValidate only occurs if the user edited the value of the cell that is about to lose focus. The OnValidate event handler can verify the value the user supplied,and if it is not acceptable,raise an exception.

例如:

uses
  SysConsts;

procedure TMyForm.lstValParamValuesValidate(Sender: TObject; ACol,ARow: Integer; const KeyName: String; const KeyValue: String);
var
  ValueType: string;
  sIgnored: Single;
  dIgnored: Double;
begin
  if KeyValue = '' then Exit;

  ValueType := parser.sParams.Values[KeyName];

  if ValueType = 'int' then
    StrToInt(KeyValue)

  else if ValueType = 'float' then
  begin
    if not TryStrToFloat(KeyValue,sIgnored) then
      raise EConvertError.CreateFmt(SInvalidFloat,[KeyValue]);
  end

  else if ValueType = 'double' then
  begin
    if not TryStrToFloat(KeyValue,dIgnored) then
      raise EConvertError.CreateFmt(SInvalidFloat,[KeyValue]);
  end

  // etc...
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读