gpt4 book ai didi

javascript - 当字段值为空时,表示太短! (Javascript)

转载 作者:行者123 更新时间:2023-11-28 20:27:42 24 4
gpt4 key购买 nike

我将尽力解释这一点。好的,这是我的代码:

function validation() {

var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var subject = document.getElementById("subject").value;
var message = document.getElementById("message").value;
var at = "@";
var dot = ".";
var lat = email.indexOf(at);
var lstr = email.length;
var ldot = email.indexOf(dot);

var success = true;

if (name == null || name == "") {
success = false;
document.getElementById("name-error").innerHTML = "I want to know who you are!";
}

if (document.contact.name.value.length <= 5) {
success = false;
document.getElementById("name-error").innerHTML = "Full name, please!";
}

if (email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(at) == lstr) {
success = false;
document.getElementById("email-error").innerHTML = "That's not an email!";
}

if (email == null || email == "") {
success = false;
document.getElementById("email-error").innerHTML = "Give me your email!";
}

if (document.contact.email.value.length <= 8) {
success = false;
document.getElementById("email-error").innerHTML = "Email is too short!";
}

if (subject == null || subject == "") {
success = false;
document.getElementById("subject-error").innerHTML = "I need a subject!";
}

if (document.contact.subject.value.length <= 5) {
success = false;
document.getElementById("subject-error").innerHTML = "A longer subject would be nice.";
}

if (message == null || message == "") {
success = false;
document.getElementById("message-error").innerHTML = "Don't forget your message!";
}

if (document.contact.message.value.length <= 30) {
success = false;
document.getElementById("message-error").innerHTML = "Your message is too concise!";
}

return success;

}

我的问题是,即使 id 的值为空,它仍然说它太短了。例如,当我单击“发送”按钮时,它说名称字段太短,即使它是空的。我想我说过如果 name == ""(为空),请说“我想知道你是谁!”。但现在,它只是说,“请输入全名”,如果名称的值 <= 5,则支持显示...而不是当它为空时。明白我在说什么吗?

最佳答案

将其变成elseif,而不是有两个单独的独立条件:

if(name == null || name == "") {
success = false;
document.getElementById("name-error").innerHTML = "I want to know who you are!";
}
else if(document.contact.name.value.length <= 5) {
success = false;
document.getElementById("name-error").innerHTML = "Full name, please!";
}

关于javascript - 当字段值为空时,表示太短! (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17058839/

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