JavaScript的if函数仅在存在一个变量时进行检查,尽管使用了||运算符。

huangapple go评论104阅读模式
英文:

Javascript if function only checks for one variable despite ||

问题

我使用PHP、JavaScript和HTML来创建一个网站,已登录的用户可以在聊天框中发表评论。我试图在创建帐户时验证输入的电子邮件,通过检查是否包含'@'和'.'。函数调用在HTML文件中。表单(包装器和主体除外)如下所示:

  1. <form id="form" onsubmit="HandleSubmit(event)" action="accountcreation.php" method="POST">
  2. <label for="email">Email:</label>
  3. <br>
  4. <input type="text" id="email" name="email" required>
  5. <br>
  6. <label for="password">Password:</label>
  7. <input type="password" name="password" required>
  8. <br>
  9. <br>
  10. <button type="submit">Create account</button>
  11. </form>

函数在下面被调用:

  1. <script src="validate.js"></script>
  2. <script>
  3. function HandleSubmit(e) {
  4. console.log("OnSubmit");
  5. // Validation
  6. if (!ValidateEmail("email")) {
  7. e.preventDefault();
  8. alert("Invalid input of email");
  9. return false;
  10. }
  11. }
  12. </script>

在validate.js文件中,函数构造如下:

  1. function ValidateEmail(emailinputId) {
  2. const elem = document.getElementById(emailinputId);
  3. if (!elem.value.includes(".") || !elem.value.includes("@")) {
  4. return false;
  5. }
  6. return true;
  7. }

该网页仅在缺少'@'符号时发送警报,但对缺少'.'没有问题。
我需要在一天内解决这个问题,所以我在祈求救世主。

我尝试过使用'&&'和'||',但没有成功。
我尝试在同一个函数内将'.'和'@'的检查分开为不同的if语句,也没有成功。

我将其更改为另一种版本:

  1. function ValidateEmail(emailinputId) {
  2. const elem = document.getElementById(emailinputId);
  3. if (("." in elem) && ("@" in elem)) {
  4. return true;
  5. }
  6. return false;
  7. }

并以相同的方式调用它。

发生了相同的情况。问题是什么?

英文:

I am using php, javascript and html to create a website where logged in users can comment in a chatbox. I am trying validate the input email during account creation by checking if it includes '@' and '.'. The call to the function is within an html file. The form (wrapper and body excluded looks like this:

  1. &lt;form id=&quot;form&quot; onsubmit=&quot;HandleSubmit(event)&quot; action=&quot;accountcreation.php&quot; method=&quot;POST&quot;&gt;
  2. &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;
  3. &lt;br&gt;
  4. &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; required&gt;
  5. &lt;br&gt;
  6. &lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
  7. &lt;input type=&quot;password&quot; name=&quot;password&quot; required&gt;
  8. &lt;br&gt;
  9. &lt;br&gt;
  10. &lt;button type=&quot;submit&quot;&gt;Create account&lt;/button&gt;
  11. &lt;/form&gt;

The function is being called below:

  1. &lt;script src=&quot;validate.js&quot;&gt;&lt;/script&gt;
  2. &lt;script&gt;
  3. function HandleSubmit(e) {
  4. console.log(&quot;OnSubmit&quot;);
  5. // Validation
  6. if (!ValidateEmail(&quot;email&quot;)) {
  7. e.preventDefault();
  8. alert(&quot;Invalid input of email&quot;);
  9. return false;
  10. }
  11. }
  12. &lt;/script&gt;

In the validate.js file the function is contructed like this:

  1. function ValidateEmail(emailinputId) {
  2. const elem = document.getElementById(emailinputId);
  3. if ( !elem.value.includes(&quot;.&quot;) || !elem.value.includes(&quot;@&quot;)) {
  4. return false;
  5. }
  6. return true;
  7. }

The webpage only sends and alert when the '@' sign is missing but is fine with '.' missing.
I need to solve this within a day so I'm praying for a saviour.

I have tried with both && and || which didn't work.
I tried separating the '.' and '@' checks into separate if statements within the same function which didn't work.

I changed it to an alternate version:

  1. function ValidateEmail(emailinputId) {
  2. const elem = document.getElementById(emailinputId);
  3. if ( (&quot;.&quot; in elem ) &amp;&amp; (&quot;@&quot; in elem)) {
  4. return true;
  5. }
  6. return false;
  7. }

And called to it in the same way.

Same thing happens. What is the issue?

答案1

得分: 0

你遇到的问题出现在你对逻辑运算符的使用上。在你的第一个ValidateEmail函数中,你使用了AND运算符(&&),意味着两个条件都必须为假才会返回假。因此,如果任何一个字符(“.”或“@”)存在,它会返回真,这不是你想要的行为。

这是你函数的修改版本:

  1. function ValidateEmail(emailinputId) {
  2. const elem = document.getElementById(emailinputId);
  3. return elem.value.includes(" . ") &amp;&amp; elem.value.includes(" @ ");
  4. }
  5. function HandleSubmit(e) {
  6. console.log(" OnSubmit ");
  7. // 验证
  8. if (!ValidateEmail(" email ")) {
  9. e.preventDefault();
  10. alert(" Invalid input of email ");
  11. return false;
  12. }
  13. }
  1. <form id="form" onsubmit="HandleSubmit(event)" action="accountcreation.php" method="POST">
  2. <label for="email">Email:</label>
  3. <br>
  4. <input type="text" id="email" name="email" required>
  5. <br>
  6. <label for="password">Password:</label>
  7. <br>
  8. <input type="password" name="password" required>
  9. <br>
  10. <br>
  11. <button type="submit">Create account</button>
  12. </form>
英文:

The issue you're experiencing lies within your usage of the logical operators in your condition. In your first ValidateEmail function, you're using the AND operator (&&), meaning both conditions have to be false to return false. So if any of the characters (either "." or "@") is present, it would return true which is not the behavior you want.

Here's a modified version of your function:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function ValidateEmail(emailinputId) {
  2. const elem = document.getElementById(emailinputId);
  3. return elem.value.includes(&quot;.&quot;) &amp;&amp; elem.value.includes(&quot;@&quot;);
  4. }
  5. function HandleSubmit(e) {
  6. console.log(&quot;OnSubmit&quot;);
  7. // Validation
  8. if (!ValidateEmail(&quot;email&quot;)) {
  9. e.preventDefault();
  10. alert(&quot;Invalid input of email&quot;);
  11. return false;
  12. }
  13. }

<!-- language: lang-html -->

  1. &lt;form id=&quot;form&quot; onsubmit=&quot;HandleSubmit(event)&quot; action=&quot;accountcreation.php&quot; method=&quot;POST&quot;&gt;
  2. &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;
  3. &lt;br&gt;
  4. &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; required&gt;
  5. &lt;br&gt;
  6. &lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
  7. &lt;br&gt;
  8. &lt;input type=&quot;password&quot; name=&quot;password&quot; required&gt;
  9. &lt;br&gt;
  10. &lt;br&gt;
  11. &lt;button type=&quot;submit&quot;&gt;Create account&lt;/button&gt;
  12. &lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 09:41:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定