使用js2.点击登录按钮 弹框提示 “您已经在登录界面” 3.手机号文本框的验证 为失焦验证 4.验证手机号的长度为11位的数字 并且第一位必须为1 5.手机号验证失败 则清空手机号文本框所有内容 6.验证码为随机的四位英文 7.点击验证码进行随机切换 8.验证码验证失败 则清空验证码文本框的内容 9.验证码的验证不区分大小写 10.点击发送验证码 出现20s倒计时 按钮禁用 11.倒计时结束后 按钮禁用解除
时间: 2024-03-19 16:43:06 浏览: 65
好的,您的第一个问题是关于前端开发的,我可以帮您解答。针对您的需求,可以使用以下代码实现:
1. 点击登录按钮弹框提示 "您已经在登录界面"
```javascript
const loginBtn = document.querySelector('#login-btn');
loginBtn.addEventListener('click', () => {
alert('您已经在登录界面');
});
```
2. 手机号文本框的验证为失焦验证
```javascript
const phoneInput = document.querySelector('#phone-input');
phoneInput.addEventListener('blur', () => {
// 手机号验证逻辑
});
```
3. 验证手机号的长度为11位的数字,并且第一位必须为1
```javascript
const phoneInput = document.querySelector('#phone-input');
phoneInput.addEventListener('blur', () => {
const phone = phoneInput.value;
const reg = /^1\d{10}$/;
if (!reg.test(phone)) {
phoneInput.value = '';
alert('请输入11位数字手机号,第一位必须为1');
}
});
```
4. 手机号验证失败则清空手机号文本框所有内容
```javascript
const phoneInput = document.querySelector('#phone-input');
phoneInput.addEventListener('blur', () => {
const phone = phoneInput.value;
const reg = /^1\d{10}$/;
if (!reg.test(phone)) {
phoneInput.value = '';
alert('请输入11位数字手机号,第一位必须为1');
}
});
```
5. 验证码为随机的四位英文
```javascript
const codeBtn = document.querySelector('#code-btn');
const codeInput = document.querySelector('#code-input');
const codes = ['ABCD', 'EFGH', 'IJKL', 'MNOP'];
codeBtn.addEventListener('click', () => {
const code = codes[Math.floor(Math.random() * codes.length)];
codeBtn.textContent = code;
});
```
6. 点击验证码进行随机切换
```javascript
const codeBtn = document.querySelector('#code-btn');
const codeInput = document.querySelector('#code-input');
const codes = ['ABCD', 'EFGH', 'IJKL', 'MNOP'];
codeBtn.addEventListener('click', () => {
const code = codes[Math.floor(Math.random() * codes.length)];
codeBtn.textContent = code;
});
```
7. 验证码验证失败则清空验证码文本框的内容,并弹出提示
```javascript
const codeBtn = document.querySelector('#code-btn');
const codeInput = document.querySelector('#code-input');
const codes = ['ABCD', 'EFGH', 'IJKL', 'MNOP'];
codeBtn.addEventListener('click', () => {
const code = codes[Math.floor(Math.random() * codes.length)];
codeBtn.textContent = code;
});
codeInput.addEventListener('blur', () => {
const inputCode = codeInput.value;
const code = codeBtn.textContent;
if (inputCode.toLowerCase() !== code.toLowerCase()) {
codeInput.value = '';
alert('验证码错误,请重新输入');
}
});
```
8. 点击发送验证码出现20s倒计时,按钮禁用
```javascript
const codeBtn = document.querySelector('#code-btn');
const codeInput = document.querySelector('#code-input');
const codes = ['ABCD', 'EFGH', 'IJKL', 'MNOP'];
let count = 20;
let timer = null;
codeBtn.addEventListener('click', () => {
const code = codes[Math.floor(Math.random() * codes.length)];
codeBtn.textContent = code;
codeBtn.disabled = true;
timer = setInterval(() => {
count--;
if (count === 0) {
clearInterval(timer);
codeBtn.disabled = false;
codeBtn.textContent = '发送验证码';
count = 20;
} else {
codeBtn.textContent = `${count}s后重新发送`;
}
}, 1000);
});
codeInput.addEventListener('blur', () => {
const inputCode = codeInput.value;
const code = codeBtn.textContent;
if (inputCode.toLowerCase() !== code.toLowerCase()) {
codeInput.value = '';
alert('验证码错误,请重新输入');
}
});
```
9. 验证码的验证不区分大小写
```javascript
const codeBtn = document.querySelector('#code-btn');
const codeInput = document.querySelector('#code-input');
const codes = ['ABCD', 'EFGH', 'IJKL', 'MNOP'];
let count = 20;
let timer = null;
codeBtn.addEventListener('click', () => {
const code = codes[Math.floor(Math.random() * codes.length)];
codeBtn.textContent = code;
codeBtn.disabled = true;
timer = setInterval(() => {
count--;
if (count === 0) {
clearInterval(timer);
codeBtn.disabled = false;
codeBtn.textContent = '发送验证码';
count = 20;
} else {
codeBtn.textContent = `${count}s后重新发送`;
}
}, 1000);
});
codeInput.addEventListener('blur', () => {
const inputCode = codeInput.value;
const code = codeBtn.textContent;
if (inputCode.toLowerCase() !== code.toLowerCase()) {
codeInput.value = '';
alert('验证码错误,请重新输入');
}
});
```
10. 倒计时结束后,按钮禁用解除
```javascript
const codeBtn = document.querySelector('#code-btn');
const codeInput = document.querySelector('#code-input');
const codes = ['ABCD', 'EFGH', 'IJKL', 'MNOP'];
let count = 20;
let timer = null;
codeBtn.addEventListener('click', () => {
const code = codes[Math.floor(Math.random() * codes.length)];
codeBtn.textContent = code;
codeBtn.disabled = true;
timer = setInterval(() => {
count--;
if (count === 0) {
clearInterval(timer);
codeBtn.disabled = false;
codeBtn.textContent = '发送验证码';
count = 20;
} else {
codeBtn.textContent = `${count}s后重新发送`;
}
}, 1000);
});
codeInput.addEventListener('blur', () => {
const inputCode = codeInput.value;
const code = codeBtn.textContent;
if (inputCode.toLowerCase() !== code.toLowerCase()) {
codeInput.value = '';
alert('验证码错误,请重新输入');
}
});
```
以上代码可以实现您所需的功能,您可以根据自己的需求进行修改。
阅读全文