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

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

Javascript if function only checks for one variable despite ||

问题

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

<form id="form" onsubmit="HandleSubmit(event)" action="accountcreation.php" method="POST">
    <label for="email">Email:</label>
    <br>
    <input type="text" id="email" name="email" required>
    <br>
    <label for="password">Password:</label>
    <input type="password" name="password" required>
    <br>
    <br>
    <button type="submit">Create account</button>
</form>

函数在下面被调用:

<script src="validate.js"></script>
<script>
    function HandleSubmit(e) {
        console.log("OnSubmit");

        // Validation
        if (!ValidateEmail("email")) {
            e.preventDefault();
            alert("Invalid input of email");
            return false;
        }
    }
</script>

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

function ValidateEmail(emailinputId) {
    const elem = document.getElementById(emailinputId);
    if (!elem.value.includes(".") || !elem.value.includes("@")) {
        return false;
    }
    return true;
}

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

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

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

function ValidateEmail(emailinputId) {
    const elem = document.getElementById(emailinputId);
    
    if (("." in elem) && ("@" in elem)) {
        return true;
    }
    
    return false;
}

并以相同的方式调用它。

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

英文:

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:

&lt;form id=&quot;form&quot; onsubmit=&quot;HandleSubmit(event)&quot; action=&quot;accountcreation.php&quot; method=&quot;POST&quot;&gt;
                 
    &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;
    &lt;br&gt;
    &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; required&gt;
    &lt;br&gt;
    &lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; required&gt;
    &lt;br&gt;
    &lt;br&gt;
    &lt;button type=&quot;submit&quot;&gt;Create account&lt;/button&gt;
            
    &lt;/form&gt;

The function is being called below:

&lt;script src=&quot;validate.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
    function HandleSubmit(e) {
        console.log(&quot;OnSubmit&quot;);

        // Validation
        if (!ValidateEmail(&quot;email&quot;)) {
            e.preventDefault();
            alert(&quot;Invalid input of email&quot;);
            return false;
        }

    }
&lt;/script&gt;

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

function ValidateEmail(emailinputId) {

    const elem = document.getElementById(emailinputId);
    if ( !elem.value.includes(&quot;.&quot;) || !elem.value.includes(&quot;@&quot;)) {
        return false;
    }
    
    return true;

}

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:

function ValidateEmail(emailinputId) {

    const elem = document.getElementById(emailinputId);
    
    if ( (&quot;.&quot; in elem ) &amp;&amp; (&quot;@&quot; in elem)) {
        return true;
    }
    
    return false;

}

And called to it in the same way.

Same thing happens. What is the issue?

答案1

得分: 0

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

这是你函数的修改版本:

function ValidateEmail(emailinputId) {
  const elem = document.getElementById(emailinputId);
  
  return elem.value.includes(" . ") &amp;&amp; elem.value.includes(" @ ");
}

function HandleSubmit(e) {
  console.log(" OnSubmit ");

  // 验证
  if (!ValidateEmail(" email ")) {
    e.preventDefault();
    alert(" Invalid input of email ");
    return false;
  }
}
<form id="form" onsubmit="HandleSubmit(event)" action="accountcreation.php" method="POST">

  <label for="email">Email:</label>
  <br>
  <input type="text" id="email" name="email" required>
  <br>
  <label for="password">Password:</label>
    <br>
  <input type="password" name="password" required>
  <br>
  <br>
  <button type="submit">Create account</button>
</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 -->

function ValidateEmail(emailinputId) {
  const elem = document.getElementById(emailinputId);
  
  return elem.value.includes(&quot;.&quot;) &amp;&amp; elem.value.includes(&quot;@&quot;);
}

function HandleSubmit(e) {
  console.log(&quot;OnSubmit&quot;);

  // Validation
  if (!ValidateEmail(&quot;email&quot;)) {
    e.preventDefault();
    alert(&quot;Invalid input of email&quot;);
    return false;
  }
}

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

&lt;form id=&quot;form&quot; onsubmit=&quot;HandleSubmit(event)&quot; action=&quot;accountcreation.php&quot; method=&quot;POST&quot;&gt;

  &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;
  &lt;br&gt;
  &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; required&gt;
  &lt;br&gt;
  &lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;
    &lt;br&gt;
  &lt;input type=&quot;password&quot; name=&quot;password&quot; required&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;button type=&quot;submit&quot;&gt;Create account&lt;/button&gt;
&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:

确定