⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucheckvalidate.pas

📁 这是一个啤酒行业的软件
💻 PAS
字号:
unit uCheckValidate;

interface

uses
    Messages,
    Dialogs,
    Variants,
    Controls,
    Windows,
    StdCtrls,
    ComCtrls,
    SysUtils,
    Mask,
    uAdoSet,
    Classes;
Type
    TCheckValidate=class
    
    private
        _param:TStringList;
        _Ado:TuAdoSet;
        Function isValidateNumber(sourceStr:string;Dec:boolean):boolean;
        Function isValidateDate(sourceStr:string):boolean;
        Function getCount(substring:String;str:String):integer;
        Function getSubFirstPos(substring:string;sourceString:string;iCount:integer):integer;
    public
        constructor create;overload;
        Function Do_Control(_ctrlType:TControl;_dataType:integer):boolean;overload;
        Function Do_Control(_ctrlType:TControl;_Sql:string):boolean;overload;
        Function Do_Control(_ctrlType:TControl;_dataType:integer;errorMsg:String):boolean;overload;
    protected

    published

end;

implementation
{
    功能:构造函数:
    参数:
    用法:
}
constructor TCheckValidate.create;
begin
    _Ado:=TuAdoSet.Create('SBmanage',1);
end;
{
    功能:处理非法的控件内容
    参数:
          _ctrlType:控件类型;目前支持TEdit.TCombobox,TMaskEdit
          _dataType:检测数据类型 1:代表数据不能为空
                                 2:代表数据必须是数值
                                 3:代表数据是有效的日期
    用法:_chek.do_control(edit1,1,'单据号不能为空');
}
Function TCheckValidate.Do_Control(_ctrlType:TControl;_dataType:integer;errorMsg:String):boolean;
begin
    result:=true;
    if (_ctrlType is TEdit) then
    begin
       if _dataType=1 then
       begin
          if TEdit(_ctrlType).text='' then
          begin
             showmessage(errorMsg);
             //System.exit;
             result:=false;
          end;
       end;//fi _data
       if _dataType=2 then
       begin

          if not isValidateNumber(TEdit(_ctrlType).text,true) then
          begin
             showmessage(errorMsg);
             result:=false;
          end;
       end;//fi _data
       if _dataType=3 then
       begin
          if not isValidateDate(TEdit(_ctrlType).text) then
          begin
             showmessage(errorMsg);
             result:=false;
          end;
       end;//fi _data
    end;
    if (_ctrlType is TCombobox) then
    begin
       if (_dataType=1) then
       begin
          if TCombobox(_ctrlType).text='' then
          begin
             showmessage(errorMsg);
             result:=false;
          end;
       end;
       if _dataType=2 then
       begin

          if not isValidateNumber(TEdit(_ctrlType).text,true) then
          begin
             showmessage(errorMsg);
             result:=false;
          end;
       end;//fi _data
    end;///
    if (_ctrlType is TMaskEdit) then
    begin
       if (_dataType=1) then
       begin
           if TMaskEdit(_ctrlType).text='' then
           begin
              showmessage(errorMsg);
              result:=false;
           end;

       end;
       if (_dataType=2) then
       begin
           if not isValidateDate(TMaskEdit(_ctrlType).text) then
           begin
              showmessage(errorMsg);
              result:=false;
           end;//
       end;

    end;///if TMaskedit
end;
{
    功能:处理非法的控件内容
    参数:
          _ctrlType:控件类型;目前支持TEdit.TCombobox,TMaskEdit
          _dataType:检测数据类型 1:代表数据不能为空
                                 2:代表数据必须是数值
                                 3:代表数据是有效的日期
    用法:_chek.do_control(edit1,1);
}
Function TCheckValidate.Do_Control(_ctrlType:TControl;_dataType:integer):boolean;
begin
    result:=true;
    if (_ctrlType is TEdit) then
    begin
       if _dataType=1 then
       begin
          if TEdit(_ctrlType).text='' then
          begin
             showmessage('文本框的内容不能为空...');
             //System.exit;
             result:=false;
          end;
       end;//fi _data
       if _dataType=2 then
       begin

          if not isValidateNumber(TEdit(_ctrlType).text,true) then
          begin
             showmessage('非法的数值...');
             result:=false;
          end;
       end;//fi _data
       if _dataType=3 then
       begin
          if not isValidateDate(TEdit(_ctrlType).text) then
          begin
             showmessage('不正确的日期格式...');
             result:=false;
          end;
       end;//fi _data
    end;
    if (_ctrlType is TCombobox) then
    begin
       if (_dataType=1) then
       begin
          if TCombobox(_ctrlType).text='' then
          begin
             showmessage('下拉框的内容不能为空...');
             result:=false;
          end;
       end;
       if _dataType=2 then
       begin

          if not isValidateNumber(TEdit(_ctrlType).text,true) then
          begin
             showmessage('非法的数值...');
             result:=false;
          end;
       end;//fi _data
    end;///
    if (_ctrlType is TMaskEdit) then
    begin
       if (_dataType=1) then
       begin
           if TMaskEdit(_ctrlType).text='' then
           begin
              showmessage('日期框的内容不能为空...');
              result:=false;
           end;

       end;
       if (_dataType=2) then
       begin
           if not isValidateDate(TMaskEdit(_ctrlType).text) then
           begin
              showmessage('不正确的内容格式...');
              result:=false;
           end;//
       end;

    end;///if TMaskedit
end;
{
    功能: 判断唯一条件的记录是否存在 (只处理一个条件的)
    参数:
          _ctrlType:控件类型
          _Sql:查询的Sql语句
    用法:
}
Function TCheckValidate.Do_Control(_ctrlType:TControl;_Sql:string):boolean;
begin
    try
      _param:=TStringList.Create;
      result:=false;
      if getCount(':',_Sql)<>1 then
      begin
         showmessage('参数个数不正确');
         System.Exit;
      end;
      if (_ctrlType is TEdit) then
      begin
         _param.Clear;
         _param.Add(trim(TEdit(_ctrlType).text));
         if (_Ado.isRecordExist(_Sql,_param)) then
         begin
            showmessage('记录已经存在...');
            result:=true;
         end;
      end;

      if (_ctrlType is TCombobox) then
      begin
         _param.Clear;
         _param.Add(trim(TCombobox(_ctrlType).text));
         if (_Ado.isRecordExist(_Sql,_param)) then
         begin
            showmessage('记录已经存在...');
            System.Exit;
            result:=true;
         end;
      end;

      if (_ctrlType is TMaskEdit) then
      begin
         _param.Clear;
         _param.Add(trim(TMaskEdit(_ctrlType).text));
         if (_Ado.isRecordExist(_Sql,_param)) then
         begin
            showmessage('记录已经存在...');
            System.Exit;
            result:=true;
         end;
      end;
    Finally
      
    end;
end;
{
    功能:判断是否为有效的数值
    参数:sourceStr:要输入的字符串
          Dec:是否带小数点
    用法:
}
Function TCheckValidate.isValidateNumber(sourceStr:string;Dec:boolean):boolean;
var
   a:integer;
   s:string;
begin
   s:=trim(sourceStr);
   result:=true;
   for a:=1 to length(s) do
   begin
       if (Dec) then
       begin
          if not (s[a] In ['0'..'9','.']) then
          begin
             result:=false;
             exit;
          end;//if not
       end//if else dec
       else
       begin
          if not (s[a] In ['0'..'9']) then
          begin
             result:=false;
             exit;
          end;//if not
       end;//if Dec
   end;//for
end;
{
    功能: 判断是否为有效的日期格式
    参数:
         yyyy-mm-dd
         yyyy/mm/dd
         yy/m/d
         yy-mm-dd
    用法:
}
Function TCheckValidate.isValidateDate(sourceStr:string):boolean;
var
   s:string;
   se:string;
   _firstPos,_firstPos1,_firstPos2:integer;
   _secondPos,_secondPos1,_secondPos2:integer;
   _year,_month,_day:string;
   _year1,_month1,_day1:string;
   _year2,_month2,_day2:string;
   ////增加一种情况即:yyyy年mm月dd日
   _YearPos:integer;///年位置
   _MonthPos:integer;///月位置
   _dayPos:integer;////日位置
   _Len:integer;////字符串总长度
begin
   s:=trim(sourceStr);
   /////对于分割符'-'的//////
   _firstPos:=getSubFirstPos('-',s,1);
   _secondPos:=getSubFirstPos('-',s,2);
   _year:=copy(s,1,_firstPos-1);///年
   _month:=copy(s,_firstPos+1,_secondPos-_firstPos-1);//月
   _day:=copy(s,_secondPos+1,length(s)-_secondPos);///日
   result:=true;
   if (getCount('-',s)=2) then
   begin
      ///yyyy-mm-dd格式/////////
      if (length(trim(_year))=4) and (length(trim(_month))=2)  and  (length(trim(_day))=2) then
      begin
         if (isValidateNumber(_year,false)) and (isValidateNumber(_month,false)) and  (isValidateNumber(_day,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false;
      ///yy-mm-dd格式/////////
      if (length(trim(_year))=2) and (length(trim(_month))=2)  and  (length(trim(_day))=2) then
      begin
         if (isValidateNumber(_year,false)) and (isValidateNumber(_month,false)) and  (isValidateNumber(_day,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false;
   end;//if getCount

   /////对于分割符'/'的//////
   _firstPos1:=getSubFirstPos('/',s,1);
   _secondPos1:=getSubFirstPos('/',s,2);
   _year1:=copy(s,1,_firstPos1-1);///年
   _month1:=copy(s,_firstPos1+1,_secondPos1-_firstPos1-1);//月
   _day1:=copy(s,_secondPos1+1,length(s)-_secondPos1);///日
   if (getCount('/',s)=2) then
   begin
      ///yyyy/mm/dd//////////
      if (length(trim(_year1))=4) and (length(trim(_month1))=2)  and  (length(trim(_day1))=2) then
      begin
         if (isValidateNumber(_year1,false)) and (isValidateNumber(_month1,false)) and  (isValidateNumber(_day1,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false;
      ///yy/mm/dd//////////
      if (length(trim(_year1))=2) and (length(trim(_month1))=2)  and  (length(trim(_day1))=2) then
      begin
         if (isValidateNumber(_year1,false)) and (isValidateNumber(_month1,false)) and  (isValidateNumber(_day1,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
        ///yy/m/d//////////
      if (length(trim(_year1))=2) and (length(trim(_month1))=1)  and  (length(trim(_day1))=1) then
      begin
         if (isValidateNumber(_year1,false)) and (isValidateNumber(_month1,false)) and  (isValidateNumber(_day1,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false; result:=false;

   end;//if getCount

   /////对于分割符'.'的//////
   _firstPos2:=getSubFirstPos('.',s,1);
   _secondPos2:=getSubFirstPos('.',s,2);
   _year2:=copy(s,1,_firstPos2-1);///年
   _month2:=copy(s,_firstPos2+1,_secondPos2-_firstPos2-1);//月
   _day2:=copy(s,_secondPos2+1,length(s)-_secondPos2);///日
   if (getCount('.',s)=2) then
   begin
      ////yyyy.mm.dd//////////////
      if (length(trim(_year2))=4) and (length(trim(_month2))=2)  and  (length(trim(_day2))=2) then
      begin
         if (isValidateNumber(_year2,false)) and (isValidateNumber(_month2,false)) and  (isValidateNumber(_day2,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false;
      ////yy.mm.dd//////////////
      if (length(trim(_year2))=2) and (length(trim(_month2))=2)  and  (length(trim(_day2))=2) then
      begin
         if (isValidateNumber(_year2,false)) and (isValidateNumber(_month2,false)) and  (isValidateNumber(_day2,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false;
      ////yy.m.d//////////////
      if (length(trim(_year2))=2) and (length(trim(_month2))=1)  and  (length(trim(_day2))=1) then
      begin
         if (isValidateNumber(_year2,false)) and (isValidateNumber(_month2,false)) and  (isValidateNumber(_day2,false)) then
         begin
            result:=true;
         end
         else
            result:=false;
      end//if
      else
         result:=false;
   end;//if getCount

   /////增加一种情况即:yyyy年mm月dd日///////
   _yearPos:=pos('年',s);
   _monthPos:=pos('月',s);
   _dayPos:=pos('日',s);
   _len:=length(s);
   if (_yearPos>0)  and  (_MonthPos>0)  and (_dayPos>0)  and  (_len=14) then
   begin
      result:=true;
   end;
end;
{
  功能: 返回要查询的子串在总字符串中出现的次数
  参数:
        substring:要查询的子串
        str:总字符串
  用法: getSubstringCount(':','aaa=:a and  bbb=:b')
        返回':'在字符串'aaa=:a and  bbb=:b'中出现的次数
}
Function TCheckValidate.getCount(substring:string;str:string):integer;
var
   _a:TStringList;
   i:integer;
   _Substr:string;
begin
   _a:=TStringList.Create;
   _a.Clear;
   try

       _Substr:=trim(str);
       i:=0;
       while  pos(substring,_Substr)>0 do
       begin
              inc(i);
              delete(_Substr,1,pos(substring,_Substr));
              _a.Add(inttostr(i))
       end;//i
       result:=_a.Count;
   Finally
       _a.Free;
   end;
end;
Function TCheckValidate.getSubFirstPos(substring:string;sourceString:string;iCount:integer):integer;
var
   sub:string;
   s:string;
   i:integer;
   _Count:integer;
begin
   sub:=trim(substring);
   s:=trim(sourceString);
   _Count:=0;
   for i:=1 to iCount do
   begin
       if pos(sub,s)>0 then
       begin
          _Count:=_Count+pos(sub,s);
          delete(s,1,pos(sub,s));
       end;//if pos
   end;//for
   result:=_Count;
end;
{
  /**类结束/////////////
  **///////////////////
/}
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -