gpt4 book ai didi

javascript - checkForm() 只输入第一个 if()

转载 作者:行者123 更新时间:2023-12-03 12:27:29 25 4
gpt4 key购买 nike

我正在尝试制定一个寄存器公式,并且只有在检查所有输入是否正确/可能后才应发布。所以我在这里找到了一个很好的德语教程:

http://de.selfhtml.org/javascript/beispiele/formulareingaben.htm

..并尝试从中获取我需要的东西,如下所示:

<script type="text/javascript">
function checkForm(){
if(document.getElementById("email").value =="" || eMailToCheck.indexOf("@") == -1){
alert("Bitte Emailadresse eingeben");
document.getElementById("email").focus();
return false;
}

if(document.getElementById("username").value ==""){
alert("Bitte Usernamen eingeben");
document.getElementById("username").focus();
return false;
}

if((document.getElementById("passwordOne").value != document.getElementById("passwordTwo").value)){
alert("Passwörter stimmen nicht überein");
document.getElementById("passwordOne").focus();
return false;
}

if((document.getElementById("passwordOne").value == "") || (document.getElementById("passwordTwo").value == "")){
alert("Bitte beide Passwortfelder ausfüllen");
document.getElementById("passwordOne").focus();
return false;
}

if((document.getElementById("passwordOne").value.length < 8) || (document.getElementById("passwordTwo").value.length < 8)){
alert("Passwort muss mind. 8 Zeichen beinhalten");
document.getElementById("passwordOne").focus();
return false;
}

}
</script>

在html的头部并通过

调用它
<form action="profile.html" onsubmit="return checkForm()" method="post" name="Formular">

效果令人印象深刻:

调用该函数并进入第一个 if() block 。但前提是我有另一个 if() 或下一行中没有任何内容。如果我的行中有一个无条件的命令,它就不再起作用。

只有电子邮件验证有效,其他均不输入。我不明白,为什么!有人可以告诉我吗?

编辑:这是我的完整代码:

<html>
<head>
<title>BlackJackX</title>
<script type="text/javascript">
function checkForm(){
var flag = true;
if(document.getElementById("email").value =="" || eMailToCheck.indexOf("@") == -1){
alert("Bitte Emailadresse eingeben");
document.getElementById("email").focus();
flag=false;
}
else if(document.getElementById("username").value ==""){
alert("Bitte Usernamen eingeben");
document.getElementById("username").focus();
flag=false;
}
else if((document.getElementById("passwordOne").value != document.getElementById("passwordTwo").value)){
alert("Passwörter stimmen nicht überein");
document.getElementById("passwordOne").focus();
flag=false;
}
else if((document.getElementById("passwordOne").value == "") || (document.getElementById("passwordTwo").value == "")){
alert("Bitte beide Passwortfelder ausfüllen");
document.getElementById("passwordOne").focus();
flag=false;
}
else if((document.getElementById("passwordOne").value.length < 8) || (document.getElementById("passwordTwo").value.length < 8)){
alert("Passwort muss mind. 8 Zeichen beinhalten");
document.getElementById("passwordOne").focus();
flag=false;
}

return flag;
}
</script>
</head>
<body>
<div style="width:300px; height:175px; background-color:#DDDDDD; margin-left:auto; margin-right:auto; margin-top:150px; align:center;">
<p>
<p>
<form action="profile.html" onsubmit="return checkForm()" method="post">
<table align="center">
<br>
<tr>
<td style="text-align:right; width:25px; color:red">
<p id="emailValid" name="emailValid"></p>
</td>
<td>
Emailadresse:
</td>
<td>
<input name="email" id="email" type="text" size="15" maxlength="25" onblur="validateEmail()">
</td>
</tr>
<tr>
<td style="text-align:right; color: red">
<p id="usernameValid" name="usernameValid"></p>
</td>
<td>
Username:
</td>
<td>
<input name="username" id="username" type="text" size="15" maxlength="25" onblur="validateUsername()">
</td>
</tr>
<tr>
<td style="text-align:right; color:red" id="passwordOneValid" name="password1Valid">
</td>
<td>
Passwort:
</td>
<td>
<input style="25px" name="passwordOne" id="passwordOne" type="password" size="15" maxlength="16" style="height: 25px" onblur="validatePasswordOne()">
</td>
</tr>
<tr>
<td style="text-align:right; color:red" id="passwordTwoValid" name="passwordTwoValid">
</td>
<td>
..wiederholen:
</td>
<td>
<input style="25px" name="passwordFieldTwo" id="passwordTwo" type="password" size="15" maxlength="16" style="height: 25px" onkeyup="validatePasswordTwo()">
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input name="submit" id="submit" type="submit" style="width: 92px" value="Registrierung">
</td>
</tr>
</table>
</form>

<script type="text/javascript">

function validateEmail(){
var eMailToCheck = document.getElementById("email").value;
if(eMailToCheck!="" && eMailToCheck.indexOf("@") != -1){
document.getElementById("emailValid").innerHTML = "";
}
else{
document.getElementById("emailValid").innerHTML = "!";
}
}

function validateUsername(){
var usernameToCheck = document.getElementById("username").value;
if(usernameToCheck != ""){
document.getElementById("usernameValid").innerHTML = "";
}
else{
document.getElementById("usernameValid").innerHTML = "!";
}
}

function validatePasswordOne(){
var pwToCheck = document.getElementById("passwordOne").value;
if(pwToCheck != ""){
document.getElementById("passwordOneValid").innerHTML = "";
}
else{
document.getElementById("passwordOneValid").innerHTML = "!";
}
if(pwToCheck.length < 8 && pwToCheck != ""){
document.getElementById("passwordOneValid").innerHTML = "8 >";
}
}

function validatePasswordTwo(){
var pwToCheck = document.getElementById("passwordTwo").value;
if(pwToCheck != document.getElementById("passwordOne").value){
document.getElementById("passwordTwoValid").innerHTML = "!=";
}
else if(pwToCheck == document.getElementById("passwordOne").value){
document.getElementById("passwordTwoValid").innerHTML = "";
}
}
</script>
</div>
</body>
</html>

最佳答案

你应该在函数末尾返回,否则它退出得太早(返回退出它)。

function checkForm(){
var result = true;
if(document.getElementById("email").value =="" || eMailToCheck.indexOf("@") == -1){
alert("Bitte Emailadresse eingeben");
document.getElementById("email").focus();
result=false;
}

if(document.getElementById("username").value ==""){
alert("Bitte Usernamen eingeben");
document.getElementById("username").focus();
result=false;
}

if((document.getElementById("passwordOne").value != document.getElementById("passwordTwo").value)){
alert("Passwörter stimmen nicht überein");
document.getElementById("passwordOne").focus();
result=false;
}

if((document.getElementById("passwordOne").value == "") || (document.getElementById("passwordTwo").value == "")){
alert("Bitte beide Passwortfelder ausfüllen");
document.getElementById("passwordOne").focus();
result=false;
}

if((document.getElementById("passwordOne").value.length < 8) || (document.getElementById("passwordTwo").value.length < 8)){
alert("Passwort muss mind. 8 Zeichen beinhalten");
document.getElementById("passwordOne").focus();
result=false;

}
return result;
}

关于javascript - checkForm() 只输入第一个 if(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24120124/

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