java身份证号验证程序小例子


import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.util.Calendar;

/**IdCard.java
  @src  http://eric-619.iteye.com/blog/694014
*@author eric
*@time  6/3/2010*/

public class IdCard extends JFrame{
   TextField tf1 = new TextField();
   TextField tf2 = new TextField();
   TextField tf3 = new TextField();
   TextField tf4 = new TextField();
   TextField tf5 = new TextField();
   TextField tf6 = new TextField();
   JLabel jl1 = new JLabel();
   JLabel jl2 = new JLabel();
   JLabel jl3 = new JLabel();
   JLabel jl4 = new JLabel();
   JLabel jl5 = new JLabel();
   JButton jb1 = new JButton("查询");
   JButton jb2 = new JButton("重写");
   JPanel jp1 = new JPanel();
   JPanel jp2 = new JPanel();
  
   public static void main(String[] args) throws IOException{
       new IdCard().frameLaunch();
     }

   public void frameLaunch(){
        setBounds(250,250,400,200);
        setTitle("eric---java身份验证程序");
        this.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
               System.exit(0);
           }
        });
    jb1.addActionListener(new Tjbutton());
    jb2.addActionListener(new JbListener());
    tf6.addActionListener(new Tjbutton());
    tf6.addMouseListener(new MouseDemo());
    jp1.setLayout(new GridLayout(5,2));
    jl1.setText("                          "+"身份证号:");
    jl2.setText("                          "+"出生日期:");
    jl3.setText("                                  "+"年龄:");
    jl4.setText("                                  "+"性别:");
    jl5.setText("                 "+"本次登录时间:");
    tf6.setColumns(30);
    jp1.setBackground(Color.green);
    jp2.setBackground(Color.blue);
    this.setResizable(false);
    tf1.setEditable(false);
    tf2.setEditable(false);
    tf3.setEditable(false);
    tf4.setEditable(false);
    tf5.setEditable(false);
    tf6.setText("请输入身份证号");
    add(jp1);
    add(jp2,BorderLayout.SOUTH);
    jp1.add(jl1);
    jp1.add(tf1);
    jp1.add(jl2);
    jp1.add(tf2);
    jp1.add(jl3);
    jp1.add(tf3);
    jp1.add(jl4);
    jp1.add(tf4);
    jp1.add(jl5);
    jp1.add(tf5);
    jp2.add(tf6);
    jp2.add(jb1);
    jp2.add(jb2);
    setVisible(true);
   }
  
   private class Tjbutton implements ActionListener{
        int length;
    int y;
    int age;
    int m;
    int d;
    String strYear = null;
    String strMonth = null;
    String strDay = null;
    String strNo = null;
    String strXingBie = null;
    String sex = "男";
   
    public void actionPerformed(ActionEvent e){
        try{
           mycalendar();
        }catch(Exception e1){
           e1.printStackTrace();
         }
        }  
   
        public void mycalendar()throws IOException{
     //使用正则表达式验证欲输入的内容
     String stdin = tf6.getText().toString();
     tf6.setText("");
     length = stdin.length();
     if(length >= 15 && length < 19){
         if(length == 15){
            strYear = stdin.substring(6, ;
            strMonth = stdin.substring(8, 10);
            strDay = stdin.substring(10, 12);
            strNo = stdin.substring(12, 15);
         }else if(length == 18){
        strYear = stdin.substring(6, 10);  //372925198910180775
        strMonth = stdin.substring(10, 12);
        strDay = stdin.substring(12, 14);
        strNo = stdin.substring(14, 18);
        strXingBie = stdin.substring(16, 17);
        int xingbie = Integer.parseInt(strXingBie); 
        if(0 == xingbie % 2){
        sex = "女";
        }else{
        sex = "男";
        }
        System.out.println(strXingBie);
         }
         Calendar c = Calendar.getInstance();    //c就是总日历
             java.util.Date mydate = new java.util.Date();
             int myhours = mydate.getHours();
             int myminutes = mydate.getMinutes();
             int myseconds = mydate.getSeconds();
             String shours = Integer.toString(myhours);
             String sminutes = Integer.toString(myminutes);
             String sseconds = Integer.toString(myseconds);
         y = c.get(Calendar.YEAR);
         age = y - Integer.parseInt(strYear);
         m = c.get(Calendar.MONTH) + 1;
         d = c.get(Calendar.DATE);
         String sage = Integer.toString(age);
         String sy = Integer.toString(y);
         String sm = Integer.toString(m);
         String sd = Integer.toString(d);
         String sss = sy+'-'+sm+'-'+sd+'-'+shours+'-'+sminutes+'-'+sseconds;
         tf1.setText(stdin);
         tf2.setText(strYear+"-"+strMonth+"-"+strDay);
         tf3.setText(sage);
         tf4.setText(sex);
         tf5.setText(sss);
     }else{
         tf6.setText("您输入的身份证号码为空或有误,请重新填写!");
     }
}
      }
  
      private class JbListener implements ActionListener{
         public void actionPerformed(ActionEvent e){
            tf1.setText("");
            tf2.setText("");
            tf3.setText("");
            tf4.setText("");
            tf5.setText("");
         }
       }
  
       private class MouseDemo implements MouseListener{
   public void mouseClicked(MouseEvent e) { //鼠标按键在组件上单击(按下并释放)时调用
  
   }

   public void mouseEntered(MouseEvent e) { //鼠标进入到组件上时调用
  
   }

   public void mouseExited(MouseEvent e) { //鼠标离开组件时调用
  
   }

   public void mousePressed(MouseEvent e) { //鼠标按键在组件上按下时调用
    tf6.setText("");
   }

   public void mouseReleased(MouseEvent e) { //鼠标按钮在组件上释放时调用
  
   }
       }
    }

你可能感兴趣的:(java,C++,c,正则表达式,C#)