在我的界面有个编辑框,其格式为AA12345678901234,即前2位必须为大写字母,后面的为数字,总长度不能超过16位。
我要实现的功能,对格式进行验证,如果前2位是小写字母,自动将其转化为大写字母,其它的给出错误信息;后面的必须为数字,不符合就给出错误信息。

解决方案 »

  1.   

    function IsNumber(pStr: String):Boolean;
    var
     i: integer;
     tStr : String;
    begin
      Result := True;
      tStr := Trim(pStr);
      for i := 1 to Length(tStr) do
      begin
         if not tStr[i]  in ['0'..'9'] then
         begin
           Result := False;
           Break;
         end;  
      end;
    end;function IsLetter(pStr : String):Boolean;
    var
     i: integer;
     tStr: String;
    begin
      Result := True;
      tStr := Trim(pStr);
      for i := 1 to Length(tStr) do
      begin
        if (not tStr[i]  in ['a'..'z']) or (not tStr[i] in ['A'..'Z']) then
        begin
          Result := False;
          Break;
        end;
      end;
    end;function Check(var pStr: String):Boolean;
    var
      tStr1,tStr2: String;
    begin
      Result := True;
      if Length(pStr) > 16 then
      begin
       ShowMessage('长度大于16!');
       Result := False;
       Exit;
      end;
      tStr1 := pStr;
      tStr2 := Copy(pStr,1,2);  if not IsLetter(tStr2) then
      begin
        ShowMessage('前两个字符不是字母!');
        Result := False;
        Exit;
      end;
      
      Delete(tStr1,1,2);
      if not IsNumber(tStr1) then
      begin
        ShowMessage('后面的字符不是数字!');
        Result := False;
        Exit;
      end;
      
      pStr := UpperCase(tStr2)+tStr1;
    end;
      

  2.   


    var
      i:Integer;
      Len:Integer;
      S:string;
    begin
      S:='AA12345678901243';
      Len:=Length(S);
      if (Len<3) or (Len>16) then
      begin
        MessageBox(Handle,'长度为3~16!','提示',$40);
        exit;
      end;  for i:=1 to 2 do
        if not (S[i] in ['a'..'z','A'..'Z']) then
        begin
           MessageBox(Handle,'前两位必需为大写字母!','提示',$40);
           exit;
        end
        else
          S[i]:=UpperCase(S[i])[1];  for i:=3 to Len do
        if not (S[i] in ['0'..'9']) then
        begin
           MessageBox(Handle,'第三位开始必需为数字!','提示',$40);
           exit;
        end;
    end;
      

  3.   


    var
    str:string;
    i:integer;
    begin
    str := edit1.text;
    for i := 0 to length(str) do
      begin
         if length(str)> 16 then 
            showmessage("超长");
         if not (copy(str,1,2) in ['A'..'Z']) then
            begin
               try
                 UpperCase((copy(str,1,2));
               except
                 showmessage("非字母且不能转换");
               end;
            end;
         if not (copy(str,3,13) in ['0'..'9']) then
            showmessage("不是数字");
      end;end;
    稍微改下,LENGTH()和CPOY取的多少位突然不记得了..
      

  4.   

         if length(str)> 16 then 
            showmessage("超长");
         if copy(str,1,1) in ['a'..'z'] then
            if copy(str,2,1) in ['a'..'z'] then 
            begin
               try
                 UpperCase((copy(str,1,1));
                 UpperCase((copy(str,2,1));
               except
                 showmessage("非字母且不能转换");
               end;
            end;
         try 
           strtoint(copy(str,3,13))
         except
         showmessage("不是数字");
         end;再试试
      

  5.   

         if length(str)> 16 then 
            showmessage("超长");
         
            
            begin
                 if (copy(str,1,1) in ['a'..'z']) or (copy(str,1,1) in ['A'..'Z']) then
                    UpperCase((copy(str,1,1))
                 else  showmessage("非字母且不能转换"); 
                 if (copy(str,2,1) in ['a'..'z']) or (copy(str,2,1) in ['A'..'Z'])  then 
                    UpperCase((copy(str,2,1));
                 else showmessage("非字母且不能转换");
           
            end;
         try 
           strtoint(copy(str,3,13))
         except
         showmessage("不是数字");
         end;