使用数组、表单、字符串处理函数等实现功能: 1.输入身份证号码并获取身份证号码 2.判断身份证是否合法 2.1判断身份证位数是否是18位或是15位,否则不是合法身份证,重新输入 2.2判断身份证出身年月日是否合法 2.3判断省份位是否合法 3.获得身份证所属省份,出生年月日和性别信息。
时间: 2023-05-24 21:06:57 浏览: 128
从键盘输入一个18位或15位的身份证号码,显示器上输出年龄、性别、省份信息。
1. 实现输入身份证号码并获取身份证号码:
```html
<form>
<input type="text" id="id_num" name="id_num" placeholder="请输入身份证号码">
<button type="button" onclick="getID()">获取身份证号码</button>
</form>
<script>
function getID() {
var id_num = document.getElementById("id_num").value;
alert("身份证号码:" + id_num);
}
</script>
```
2. 实现判断身份证是否合法:
```html
<form>
<input type="text" id="id_num" name="id_num" placeholder="请输入身份证号码">
<button type="button" onclick="checkID()">检验身份证合法性</button>
</form>
<script>
function checkID() {
var id_num = document.getElementById("id_num").value;
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (reg.test(id_num) === false) {
alert("身份证格式不合法,请重新输入");
return false;
}
alert("身份证格式合法");
return true;
}
</script>
```
3. 实现获得身份证所属省份、出生年月日和性别信息:
```html
<form>
<input type="text" id="id_num" name="id_num" placeholder="请输入身份证号码">
<button type="button" onclick="getID()">获取身份证信息</button>
</form>
<script>
function getID() {
var id_num = document.getElementById("id_num").value;
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (reg.test(id_num) === false) {
alert("身份证格式不合法,请重新输入");
return false;
}
var province = {
11: "北京市",
12: "天津市",
13: "河北省",
14: "山西省",
15: "内蒙古自治区",
21: "辽宁省",
22: "吉林省",
23: "黑龙江省",
31: "上海市",
32: "江苏省",
33: "浙江省",
34: "安徽省",
35: "福建省",
36: "江西省",
37: "山东省",
41: "河南省",
42: "湖北省",
43: "湖南省",
44: "广东省",
45: "广西壮族自治区",
46: "海南省",
50: "重庆市",
51: "四川省",
52: "贵州省",
53: "云南省",
54: "西藏自治区",
61: "陕西省",
62: "甘肃省",
63: "青海省",
64: "宁夏回族自治区",
65: "新疆维吾尔自治区",
71: "台湾省",
81: "香港特别行政区",
82: "澳门特别行政区"
};
var year = "";
var month = "";
var day = "";
var sex = "";
if (id_num.length === 18) {
year = id_num.substr(6, 4);
month = id_num.substr(10, 2);
day = id_num.substr(12, 2);
sex = id_num.substr(14, 3) % 2 === 0 ? "女" : "男";
} else {
year = "19" + id_num.substr(6, 2);
month = id_num.substr(8, 2);
day = id_num.substr(10, 2);
sex = id_num.substr(12, 3) % 2 === 0 ? "女" : "男";
}
var province_code = id_num.substr(0, 2);
var province_name = province[province_code];
var message = "省份:" + province_name + "\n出生年月日:" + year + "年" + month + "月" + day + "日\n性别:" + sex;
alert(message);
return true;
}
</script>
```
阅读全文