<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.right{
color:green
}
.wrong{
color:red
}
</style>
</head>
<body>
用户名: <input type="text"id="a"><span ></span><br>
密码:<input type="password"><br>
<button>提交</button>
</body>
<script>
var input=document.getElementById("a")
var btn=document.querySelector("button")
var span=document.querySelector("span")
var r1=/^[a-zA-Z0-9-_]{6,16}$/
input.onblur=function(){
var val=this.value
var flag=r1.test(val)
if(flag){
span.innerHTML="用户输入合法"
span.className="right"
}else{
span.innerHTML="用户输入不合法"
span.className="wrong"
}
}
</script>
</html>
效果:
标签:right,span,正则表达式,javascript,innerHTML,表单,color,var,document From: https://blog.csdn.net/2401_83905000/article/details/143852508