gpt4 book ai didi

javascript - 显示需要用户名和密码的消息是什么?

转载 作者:行者123 更新时间:2023-12-01 01:34:40 25 4
gpt4 key购买 nike

我不知道如何显示需要用户名和密码的消息!

当我点击“提交”时,它会自动打开到index.php

它应该显示我需要用户名和密码

这是我的代码:

var username = document.form["vform"]["username"];
var email = document.form["vform"]["email"];
var password = document.form["vform"]["password"];
var password_confirmation = document.form["vform"]["password_confirmation"];



var name_error = document.getElementById("name_error");
var email_error = document.getElementById("email_error");
var password_error = document.getElementById("password_error");


username.addEventListener("blur", nameVerify, true);
email.addEventListener("blur", emailVerify, true);
password.addEventListener("blur", passwordVerify, true);



function Validate() {
if (username.value == "") {
username.style.border = "1px solid red";
name_error.textContent = "Username is required";
name.focus();
return false;

}


if (email.value == "") {
email.style.border = "1px solid red";
email_error.textContent = "Email is required";
email.focus();
return false;

}




if (password.value == "") {
password.style.border = "1px solid red";
password_error.textContent = "Password is required";
password.focus();
return false;

}


// Check if two password match
if (password.value != password_confirmation.value) {
password.style.border = "1px solid red";
password_confirmation.style.border = "1px solid red";
password_error.innerHTML = "The two passwords don't match";
return false;

}
}


function nameVerify() {
if (username.value != "") {
username.style.border = "1px solid #5E6E66";
name_error.innerHTML = "";
return true;
}
}


function emailVerify() {
if (email.value != "") {
email.style.border = "1px solid #5E6E66";
email_error.innerHTML = "";
return true;
}
}


function passwordVerify() {
if (password.value != "") {
password.style.border = "1px solid #5E6E66";
password_error.innerHTML = "";
return true;
}
}

我没有看到错误在哪里,我认为这很好,没有语法错误,也许缺少了一些东西。

最佳答案

确保您的表单具有 required attribute (HTML5):

<input type="text" name="username" required />
<input type="text" name="password" required />

无需 JavaScript 验证。确保也在服务器端进行验证。

关于javascript - 显示需要用户名和密码的消息是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52936999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com