简体   繁体   English

javascript表单验证即使条件匹配也总是返回true,并且应该返回false

[英]javascript form validation always returns true even when condition matches and should returns false

I'm trying to make a javascript form validation based on Vikrant 's script on jsfiddle. 我正在尝试基于jsfiddle上Vikrant的脚本进行javascript表单验证。

I have made my own script on jsfiddle too here it is pretty similar to his code but for some reason, the alert never triggers, the form is submitted even though it should not be. 我已经作出了自己的剧本上的jsfiddle过这里它是非常相似,他的代码,但由于某些原因,警报从未触发,提交表单,即使它不应该。

this is happening in my localhost too. 这也在我的本地主机中发生。

also here is the code : 这也是代码:

 function FormValidasi() { if (document.getElementById('cabang').value == "tor") { alert("Mohon pilih cabang nya"); return false; } else if (document.getElementById('tipe').value == "tor") { alert("Mohon pilih tipenya"); return false; } return (true); } 
 <form name="Formulir" method="post" action="2013.php" onsubmit="return FormValidasi();"> <input type="text" name="cabang" id="cabang" placeholder="Input cabang"> <br> <input type="text" name="tipe" id="tipe" placeholder="Input Tipe"> <input type="submit" name="submit" value="submit"> </form> 

so if I type "tor" inside both field, or just one of the field, the script should be returning false and prevent the form submitting? 因此,如果我在两个字段或其中一个字段中都键入"tor" ,则脚本应该returning false并阻止表单提交? but the form is submitted even if I typed "tor" 但是即使我输入"tor" ,表单仍会提交

The reason for not working your code is the line you've coded here as return (true); 无法使用代码的原因是您在此处编码为return (true); The code execution for your JS is check the IF conditions and return only one output from there. JS的代码执行是检查IF条件,并从那里仅返回一个输出。 From the line where you always return true means script executes the IF statement but ultimately return true even you have an error on your input fields. 从始终return true的行开始,脚本将执行IF语句,但是即使您在输入字段中出错,最终也会return true

 function FormValidasi() { if (document.getElementById('cabang').value == "tor") { alert("Mohon pilih cabang nya"); return false; } else if (document.getElementById('tipe').value == "tor") { alert("Mohon pilih tipenya"); return false; } } 
 <form name="Formulir" method="post" action="2013.php" onsubmit="return FormValidasi();"> <input type="text" name="cabang" id="cabang" placeholder="Input cabang"> <br> <input type="text" name="tipe" id="tipe" placeholder="Input Tipe"> <input type="submit" name="submit" value="submit"> </form> 

Update 1 更新1

Reasonable argument has been made by 2 users and i have update the snippet to avoid getting error as below. 2位用户提出了合理的论据,并且我已更新了代码片段,以避免出现以下错误。

Uncaught ReferenceError: FormValidasi is not defined at HTMLFormElement.onsubmit 未捕获的ReferenceError:在HTMLFormElement.onsubmit上未定义FormValidasi

This can be avoid from making the form submit validation function as global function. 可以避免使表单提交验证功能作为全局功能。

window.FormValidasi = FormValidasi;

Updated code: 更新的代码:

 window.FormValidasi = FormValidasi; function FormValidasi() { if (document.getElementById('cabang').value == "tor") { alert("Mohon pilih cabang nya"); return false; } else if (document.getElementById('tipe').value == "tor") { alert("Mohon pilih tipenya"); return false; } } 
 <form name="Formulir" method="post" action="2013.php" onsubmit="return FormValidasi();"> <input type="text" name="cabang" id="cabang" placeholder="Input cabang"> <br> <input type="text" name="tipe" id="tipe" placeholder="Input Tipe"> <input type="submit" name="submit" value="submit"> </form> 

Read more about the function error on onsubmit function is not defined 阅读有关未定义onsubmit函数的函数错误的更多信息

Update 2 更新2

Thanks to giving a thought on looking at the code again. 多亏了重新思考代码的想法。 The OP had the function correctly defined. OP具有正确定义的功能。 As the problem was his function not executing properly was entirely due to js mishap. 因为问题在于他的功能无法正确执行,完全是由于js事故。 It can be avoided by making the function global . 可以通过使函数成为global函数来避免这种情况。

To summarize the previously accepted answer and its comments, the correct code should be: 总结以前接受的答案及其评论,正确的代码应为:

Javascript Java脚本

window.FormValidasi = FormValidasi;

function FormValidasi() {
  if (document.getElementById('cabang').value == "tor") {
    alert("Mohon pilih cabang nya");
    return false;
  } else if (document.getElementById('tipe').value == "tor") {
    alert("Mohon pilih tipenya");
    return false;
  }
  return true;
}

HTML 的HTML

<form name="Formulir" method="post" action="2013.php" onsubmit="return FormValidasi();">
  <input type="text" name="cabang" id="cabang" placeholder="Input cabang">
  <br>
  <input type="text" name="tipe" id="tipe" placeholder="Input Tipe">
  <input type="submit" name="submit" value="submit">
</form>

The problem was not the last line of the function being executed to always return true as stated in the first paragraph of the accepted answer, but rather that there was a coding error preventing the function from being executed in the first place. 问题在于所执行的函数的最后一行始终如所接受答案的第一段所述始终返回true ,而是存在编码错误,导致无法首先执行该函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM