我在EDIT1中输入一个身份证号码,失去焦点后自动计算出出生年月,性别、和年龄,要对15和18位号码的有效行进行验证,年龄要求是计算出实际年龄而不是虚岁,请各位用代码解决,谢谢!

解决方案 »

  1.   

    要判断男还是女的
    问一下派出所编身份证的人,
    计算年龄最省事的方法就是用SQLServer的DateDiff(year,SDate,EDate)
      

  2.   

    18 位:
    123456ccyymmddABCI;
    ccyymmdd=出生年月日;
    YYYY=今年,
    MM=今月,
    DD=今日;
    年齡=TRUNC((YYYY-ccyymm)+0.5*SIGN(MM-mm)+0.05*SIGN(DD-dd))(歲數);if ( (ABC div 2)*2=ABC ) then
      sex:='女'
    else
      sex:='男';15 位:
    123456yymmddABC;
    ccyymmdd='19'+'mmdd';
    方法同上.
      

  3.   

    bai11(悠悠生) 他的身份证是18位的,最后一位是校验码,
    18位分辨男女要看倒数第二位。
      

  4.   

    15位:
    123456YYMMDDXXX
    18位:
    123456YYYYMMDDXXXM123456:地址
    YY or YYYY:年
    MM:月
    DD:日
    XXX:编号(奇数为男)
    M:校验位其实上边zhangqiufk(真言) 老兄都写出来了
    if ( (ABC div 2)*2=ABC ) then
      sex:='女'
    else
      sex:='男';
    不过我感觉
    if (C div 2=0) then
      sex:='女'
    else
      sex:='男';
    好点
      

  5.   

    我程序中的一段代码,你读读吧。
    其中:hm是填写号码的edit,xb是显示性别的dbcombobox,sr是得到生日的edit。procedure check_sfz(hm:Tdbedit;xb:tdbcombobox;sr:tdbedit);
    var
      lsstr,srstr:string;
      i:integer;
    begin
      lsstr:=hm.text;
      if (length(trim(lsstr))<>0) and (length(trim(lsstr))<>15) and (length(trim(lsstr))<>18) then
        if application.MessageBox('身份证号码长度无效,是否重新填写?','提示信息',MB_ICONQUESTION+mb_yesno)=idyes then
          begin
            hm.SetFocus;
            exit;
          end;  if length(trim(lsstr))=15 then
        begin
          for i:=1 to 15 do
            if (ord(lsstr[i])<48) or (ord(lsstr[i])>57) then
              begin
                application.MessageBox(pchar('身份证号码含有无效字符,请重新填写。'),'提示信息',MB_ICONinformation+mb_ok);
                hm.SetFocus;
                exit;
              end;      if (strtoint(lsstr[15]) mod 2 = 0) then
            i:=1
          else
            i:=0;
          if xb.ItemIndex=-1 then
            begin
              xb.ItemIndex:=i;
              xb.Field.AsString:=xb.Text;
              //xb.SetFocus;
            end;
          //else
            //if xb.ItemIndex<>i then
              //if application.MessageBox('性别填写有误,是否更正?','提示信息',MB_ICONQUESTION+mb_yesno)=idyes then
                //begin
                  //xb.ItemIndex:=i;
                  //xb.Field.AsString:=xb.Text;
                  //xb.SetFocus;
                //end;
          srstr:='19'+copy(lsstr,7,2)+'-'+copy(lsstr,9,2)+'-'+copy(lsstr,11,2);
          DateSeparator := '-';
          try
            strtodate(srstr);
          except
            srstr:='';
          end;
          if srstr<>'' then
            srstr:='19'+copy(lsstr,7,2)+'年'+copy(lsstr,9,2)+'月'+copy(lsstr,11,2)+'日';
          if trim(sr.Field.AsString)='' then
            begin
              if srstr<>'' then
                sr.Field.AsString:=srstr;
              //sr.SetFocus;
            end
          else
            if sr.Field.AsString<>srstr then
              //if application.MessageBox(pchar('生日填写应为'+ srstr +',是否更正?'),'提示信息',MB_ICONQUESTION+mb_yesno)=idyes then
                //begin
                if srstr<>'' then
                  sr.Field.AsString:=srstr;
                  //sr.SetFocus;
                //end;
          sr.SetFocus;
        end;
      if length(trim(lsstr))=18 then
        begin
          for i:=1 to 17 do
            if (ord(lsstr[i])<48) or (ord(lsstr[i])>57) then
              begin
                application.MessageBox(pchar('身份证号码含有无效字符,请重新填写!'),'提示信息',MB_ICONinformation+mb_ok);
                hm.SetFocus;
                exit;
              end;
          if (strtoint(lsstr[17]) mod 2 = 0) then
            i:=1
          else
            i:=0;
          if xb.ItemIndex=-1 then
            begin
              xb.ItemIndex:=i;
              xb.Field.AsString:=xb.Text;
              //xb.SetFocus;
            end;
          //else
            //if xb.ItemIndex<>i then
              //if application.MessageBox('性别填写有误,是否更正?','提示信息',MB_ICONQUESTION+mb_yesno)=idyes then
                //begin
                  //xb.ItemIndex:=i;
                  //xb.Field.AsString:=xb.Text;
                  //xb.SetFocus;
                //end;      srstr:='19'+copy(lsstr,9,2)+'-'+copy(lsstr,11,2)+'-'+copy(lsstr,13,2);
          DateSeparator := '-';
          try
            strtodate(srstr);
          except
            srstr:='';
          end;
          if srstr<>'' then
            srstr:='19'+copy(lsstr,9,2)+'年'+copy(lsstr,11,2)+'月'+copy(lsstr,13,2)+'日';      if trim(sr.Field.AsString)='' then
            begin
              if srstr<>'' then
                sr.Field.AsString:=srstr;
              //sr.SetFocus;
            end
          else
            if sr.Field.AsString<>srstr then
              //if application.MessageBox(pchar('生日填写应为'+ srstr +',是否更正?'),'提示信息',MB_ICONQUESTION+mb_yesno)=idyes then
                //begin
                if srstr<>'' then
                  sr.Field.AsString:=srstr;
                  //sr.SetFocus;
                //end;
          sr.SetFocus;
        end;
    end;
      

  6.   

    我的15位身份证丢了,补回18位的。
    变化如下:
    123456  yymmddABC
    123456ccyymmddABCI
    原有的15位保留着。
      

  7.   

    作者:闫磊 E_Mail:[email protected]根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,
    公民身份号码是特征组合码18位:由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 
    地址码表示编码对象常住户口所在县(市、旗、区)的行政区划代码。生日期码表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符。顺序码表示同一地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号。顺序码的奇数分给男性,偶数分给女性。
    15位:六位数字地址码,六位数字出生日期码,三位数字顺序码和一位数字校验码。 
    var
        codeInfoTst: Tstrings; //总
        codeTst: Tstrings; //代码
        InfoTst: Tstrings; //地址信息function identityInfo(identitycode: string): string;
    //由年月日判断是否为有效日期
        function IsDate(Year, Month, Day: string): Boolean;
        var
            st: string;
        begin        st := Year + '-' + Month + '-' + day;
            try
                StrToDate(st);
                Result := True;
            except            Result := False;        end;
        end;
        function getdetailedFromCode(code: string): string; //由身份证的前六位获得省份县市信息
        var
            Index: integer;
            St: string;
        begin
            index := codeTst.IndexOf(code);
            if index = -1 then
            begin
                Result := '';
                exit;
            end;
            st := infoTst[index]; //县
            code := copy(code, 1, 4) + '00';
            index := codeTst.IndexOf(code);
            if index = -1 then
            begin
                Result := st;
                exit;
            end;
            st := infoTst[index] + st; //市        code := copy(code, 1, 2) + '0000';
            index := codeTst.IndexOf(code);
            if index = -1 then
            begin
                Result := st;
                exit;
            end;
            st := infoTst[index] + st; //省
            Result := st;    end;var
        YLYear, YLMonth, YlDay, id, sex, code: string;
        E: integer;
        St: string;begin
        e := Length(identitycode);
        if (e <> 18) or (e <> 15) then result := '';
        if (e = 18) then
        begin
            YLYear := copy(identitycode, 7, 4);
            YlMonth := copy(identitycode, 11, 2);
            YLday := copy(identitycode, 13, 2);
            id := copy(identitycode, 18,1);
        end;
        if (e = 15) then
        begin
            YLYear := '19' + copy(identitycode, 7, 2);
            YlMonth := copy(identitycode, 9, 2);
            YLday := copy(identitycode, 11, 2);
            id := copy(identitycode, 15, 1);
        end;
        st := '';
        if IsDate(YlYear, YlMonth, Ylday) then
        begin
            st := YLYear + '年' + YlMonth + '月' + YLday + '日生'
        end;
        code := copy(identitycode, 1, 6);
        st := getdetailedFromCode(code) + ':' + st;
        if (StrToInt(id) mod 2 = 1) then
        begin
            sex := '男';
        end
        else
        begin
            sex := '女';
        end;
        st := st + ':' + sex + '性!' + Trim(IntToStr(e)) + '位证号!';
        Result := st;end;procedure TForm1.Button1Click(Sender: TObject);
    var
        i, num: integer;
        code, info: string;
    begin
        codeInfoTst := Tstringlist.Create;
        CodeTst := Tstringlist.Create;
        infotst := Tstringlist.Create;
        try
            codeInfoTst.LoadFromFile('c:\code.txt');
            //分解代码地址信息
            num := Codeinfotst.Count;
            for i := 0 to num - 1 do
            begin
                code := copy(Codeinfotst[i], 1, 6);
                Info := copy(Codeinfotst[i], 8, length(Codeinfotst[i]) - 7);
                codetst.Add(code);
                infotst.Add(info);        end;
            showmessage(identityInfo('530101700205379'));    finally
            codeTst.Free;
            infotst.Free;
            CodeinfoTst.Free;
        end;end;