<!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>登录验证</title> </head> <style> * { margin: 0; padding: 0; } #box { width: 300px; height: 300px; margin: 50px auto; } </style> <body> <div id="box"> <p>欢迎加入前端群,请输入验证码</p> <input type="text" id="input1" /> <button id="btn1">验证</button> </div> </body> <script> var btn1 = document.getElementById('btn1'); btn1.onclick = function(){ var input1 = document.getElementById('input1'); } </script> </html>修改代码 当input输入html css js 这三个其中之一便通过 输入其它或者弹出错误 输入为空则验证码为空
时间: 2023-12-03 12:03:47 浏览: 246
HTML中的<meta>标签的使用详解
5星 · 资源好评率100%
可以在 onclick 函数中加入以下代码来实现验证:
```
var input1 = document.getElementById('input1');
if(input1.value === '') {
alert('验证码不能为空!');
} else if(input1.value === 'html' || input1.value === 'css' || input1.value === 'js') {
alert('验证成功!');
} else {
alert('验证码错误!');
}
```
完整的代码如下:
```
<!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>登录验证</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 300px;
height: 300px;
margin: 50px auto;
}
</style>
<body>
<div id="box">
<p>欢迎加入前端群,请输入验证码</p>
<input type="text" id="input1" /> <button id="btn1">验证</button>
</div>
</body>
<script>
var btn1 = document.getElementById('btn1');
btn1.onclick = function(){
var input1 = document.getElementById('input1');
if(input1.value === '') {
alert('验证码不能为空!');
} else if(input1.value === 'html' || input1.value === 'css' || input1.value === 'js') {
alert('验证成功!');
} else {
alert('验证码错误!');
}
}
</script>
</html>
```
阅读全文