审查登录信息,正确账号abc,密码123。此外填写提示账号/密码不能为空,账号密码不正确等相关信息用js方法
时间: 2023-08-31 16:43:00 浏览: 106
### 回答1:
可以使用以下 JavaScript 代码来实现:
```javascript
function login() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
// 检查账号密码是否为空
if (username=="" || password=="") {
alert("账号密码不能为空!");
return false;
}
// 检查账号密码是否正确
if (username!="abc" || password!="123") {
alert("账号密码不正确!");
return false;
}
// 登录成功,跳转到首页
window.location.href = "index.html";
}
```
在 HTML 中,可以将该 JavaScript 函数绑定到登录提交按钮的点击事件上,例如:
```html
<form onsubmit="return login();">
<label>账号:</label>
<input type="text" id="username"><br>
<label>密码:</label>
<input type="password" id="password"><br>
<input type="submit" value="登录">
</form>
```
### 回答2:
你可以使用以下的JavaScript方法来实现审查登录信息、验证账号和密码的正确性,并且根据不同情况给出相应的提示信息:
```javascript
function checkLoginInfo(username, password) {
// 检查账号和密码是否为空
if (!username || !password) {
return "账号/密码不能为空";
}
// 检查账号和密码是否正确
if (username === "abc" && password === "123") {
return "登录成功";
} else {
return "账号密码不正确";
}
}
// 使用示例
var username = "abc";
var password = "123";
var loginResult = checkLoginInfo(username, password);
console.log(loginResult);
```
以上代码中,`checkLoginInfo`函数接受两个参数:`username`和`password`,分别代表输入的账号和密码。函数首先检查账号和密码是否为空,如果为空则返回"账号/密码不能为空";然后,检查账号和密码是否正确,如果正确则返回"登录成功",否则返回"账号密码不正确"。在使用时,你可以将用户输入的账号和密码作为参数传入`checkLoginInfo`函数,然后根据函数返回的结果给出相应的提示信息。在示例中,将"abc"作为账号,"123"作为密码传入`checkLoginInfo`函数,并通过`console.log`输出检查结果。
希望以上代码能够满足你的需求。
### 回答3:
使用JavaScript可以通过以下代码来执行登录信息的验证和提示:
```javascript
function validateLogin() {
var account = document.getElementById('account').value; // 获取输入的账号
var password = document.getElementById('password').value; // 获取输入的密码
if (account.trim() === '' || password.trim() === '') { // 判断账号或密码是否为空
alert('账号和密码不能为空');
} else if (account !== 'abc' || password !== '123') { // 判断账号或密码是否正确
alert('账号或密码不正确');
} else {
alert('登录成功');
}
}
```
在HTML代码中需添加相应的表单元素和按钮,并绑定上述函数:
```html
<input type="text" id="account" placeholder="请输入账号">
<input type="password" id="password" placeholder="请输入密码">
<button onclick="validateLogin()">登录</button>
```
上述代码会首先获取输入的账号和密码,然后进行判断。如果账号或密码为空,则弹出提示“账号和密码不能为空”,如果账号或密码不正确,则弹出提示“账号或密码不正确”,如果输入的账号和密码都正确,则弹出提示“登录成功”。