import java.awt.EventQueue;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class CheckPhoneNumber extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private int W[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
private int Y[]={1,0,16,9,8,7,6,5,4,3,2};
private JPanel contentPane;
private JTextField nameField;
private JTextField IdNumField;
private JTextField ageField;
private String Adress;//
private String file="C:\\Users\\Administrator\\Desktop\\IDNUMBER.txt";//文件所在的绝对路径
private String s;
private String s1;
public String Get_NumberString(String str) { //获得地址码
int i=0;
StringBuilder line=new StringBuilder();
for(i=0;i<6;i++) {
line.append(str.charAt(i));
}
return line.toString();
}
public String Get_Adress() {//获得地址
s=IdNumField.getText();
s1=Get_NumberString(s);
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = null;
try {
while((line = in.readLine()) != null){
if(s1.equals(Get_NumberString(line))) {
Adress=line.substring(7,line.length());
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Adress;
}
public String Get_Sex() {//获得性别
s=IdNumField.getText();
int len=Adress.length();
if(s.charAt(len-2)%2==0) {//为偶数
return "女性";
} else {
return "男性";
}
}
public boolean Check() {
s=IdNumField.getText();
boolean flag=false;
int i;
int sum=0;
int mod=0;
for(i=0;i<17;i++) {
sum+=(s.charAt(i)-'0')*W[i];
}
mod=sum%11;
if(mod==2) {
if('X'==s.charAt(17)||'x'==s.charAt(17)) {
flag=true;
return flag;
} else {
return flag;
}
} else {
if(Y[mod]==(s.charAt(17)-'0')) {
flag=true;
return flag;
} else {
return flag;
}
}
}
public void Create_Frame() {//创建子窗口
JFrame jf=new JFrame();
jf.setTitle("身份信息");
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(250, 250, 560, 190);
JPanel contentPane1 = new JPanel();
contentPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
jf.setContentPane(contentPane1);
contentPane1.setLayout(null);
JLabel lblip = new JLabel("姓名");
lblip.setBounds(10, 15, 122, 15);
contentPane1.add(lblip);
JTextField nameField1 = new JTextField(nameField.getText());
nameField1.setBounds(80, 10, 460, 25);
contentPane1.add(nameField1);
JButton button = new JButton("确认");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
button.setBounds(90, 119, 93, 23);
contentPane1.add(button);
JLabel label = new JLabel("所属地址");
label.setBounds(10, 87, 60, 15);
contentPane1.add(label);
JTextField IdNumField1 = new JTextField(Get_Adress());
IdNumField1.setBounds(80, 82, 460, 25);
contentPane1.add(IdNumField1);
JLabel label_1 = new JLabel("性别");
label_1.setBounds(10, 50, 122, 15);
contentPane1.add(label_1);
JTextField sexField1 = new JTextField(Get_Sex());
sexField1.setBounds(80, 45, 460, 25);
contentPane1.add(sexField1);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CheckPhoneNumber frame = new CheckPhoneNumber();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CheckPhoneNumber() {
setTitle("身份证号验证");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(250,250, 560, 190);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblip = new JLabel("姓名");
lblip.setBounds(10, 15, 122, 15);
contentPane.add(lblip);
nameField = new JTextField();
nameField.setBounds(80, 10, 460, 25);
contentPane.add(nameField);
JButton button = new JButton("验证");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setBounds(90, 119, 93, 23);
contentPane.add(button);
JLabel label = new JLabel("身份证号码");
label.setBounds(10, 87, 60, 15);
contentPane.add(label);
IdNumField = new JTextField();
IdNumField.setBounds(80, 82, 460, 25);
contentPane.add(IdNumField);
JLabel label_1 = new JLabel("年龄");
label_1.setBounds(10, 50, 122, 15);
contentPane.add(label_1);
ageField = new JTextField();
ageField.setBounds(80, 45, 460, 25);
contentPane.add(ageField);
}
protected void do_button_actionPerformed(ActionEvent e) {
String text = IdNumField.getText();// 获取用户输入
String info = check(text);// 对输入文本进行身份证验证
if(info.equals("是一个合法的身份证号码!")&&Check()) {
this.setVisible(false);
Create_Frame();
} else {
JOptionPane.showMessageDialog(null, text + "\n不是一个合法的身份证号码!");// 用对话框输出验证结果
}
}
public String check(String text){
if(text == null || text.isEmpty()){
return "请输入身份证号码!";
}
// 定义正则表达式
String regex="^(\\d{6})(18|19|20)?(\\d{2})([01]\\d)([0123]\\d)(\\d{3})(\\d|X|x)?$";
// 判断输入数据是否为身份证号码
if(text.matches(regex)){
return "是一个合法的身份证号码!";
}else{
return text + "\n不是一个合法的身份证号码!";
}
}
}
Java编写身份证号码判断及其信息
最新推荐文章于 2023-11-07 17:06:07 发布