我在写一个身份证验证程序,怎么在执行下面代码时,我输入18位身份证,它会执行15位下面的程序呢???
function TForm1.cuowu(sbh: string): integer;
//0:正确  1:位数错误   2:日期错误  3:年龄错误
begin
 cuowu:=0;
   if (length(trim(sbh))=15) or (length(trim(sbh))=18) then
   begin
     try
       if length(trim(sbh))=15 then
       begin
          strtodate('19'+copy(sbh,7,2)+'-'+copy(sbh,9,2)+'-'+copy(sbh,11,2));//在这里没加begin..end时会执行这句????
       end;
       if length(trim(sbh))=18 then
          strtodate(copy(sbh,7,4)+'-'+copy(sbh,11,2)+'-'+copy(sbh,13,2));
       cuowu:=0;
     except
       cuowu:=2;
     end;
     if length(trim(sbh))=15 then
     begin
       if (yearof(DATE)-strtoint('19'+copy(trim(sbh),7,2))<15) or (yearof(DATE)-strtoint('19'+copy(trim(sbh),7,2))>200) then
           cuowu:=3;
     end;
     if length(trim(sbh))=18 then
     begin
       if (yearof(DATE)-strtoint(copy(trim(sbh),7,4))<15) or (yearof(DATE)-strtoint(copy(trim(sbh),7,4))>120) then
           cuowu:=3;
     end;
   end
   else
     cuowu:=1;//我在这没加begin..end时也要执行这句???
end;

解决方案 »

  1.   

    我单步跟踪调试了,但在没加begin..end时确实要执行15位下程序,结果还是正确的。
      

  2.   

    LZ的delphi是不是有问题啊,正常的话是不会那样的
      

  3.   


    15位下程序是什么意思? 
        cuowu:=1;//我在这没加begin..end时也要执行这句??? 这里加不加begin end都是一样的啊
      

  4.   

    我試過了,沒有問題啊,
    樓主,你的delphi是不是有問題啊。
      

  5.   

    肯定是逻辑上出问题了,注意不加begin end的话,else与最近的if then结构配对
      

  6.   

    15位下指这句
    strtodate('19'+copy(sbh,7,2)+'-'+copy(sbh,9,2)+'-'+copy(sbh,11,2));输入18位不应该有问题的。难道真是我delphi有问题???
      

  7.   

    程序没有问题,只是在调试时delphi优化引起的(经常有此问题),只要结果正确,就没问题
    另外,你可以将优化关闭(Project options->copmiler->optimization前的勾)
      

  8.   

    还有,有关身份验证你的不全,可参考我回过的:
    http://topic.csdn.net/u/20080912/09/de7ecae2-ef07-4c3b-955e-f251a24925f5.html