<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户名正则检测</title>
</head>
<body>
用户名(首位仅支持字母大写):<input type="text" name="user" id="user" placeholder="6-12个字母或数字">
<input type="button" value="检测" onclick="checkuser()"><br>
身份证:<input type="text" name="IDnum" id="IDnum"
placeholder="14位数字">
<input type="button" value="检测" onclick="checkIDnum()">
<script>
function checkuser(){
var user=document.getElementById("user").value;
var User=/^[A-Z]{1}[A-Za-z0-9]{5,12}/;
if(User.test(user)) {
alert("书写正确");
}
else {
alert("书写错误");
}
}
function checkIDnum(){
var idnum=document.getElementById("IDnum").value;
var IDnum=/^[0-9]{14}/;
if(IDnum.test(idnum)) {
alert("书写正确");
}
else {
alert("书写错误");
}
}
</script>
</body>
</html>
效果如下图: