如果文本框为空,则返回true或显示确认信息

时间:2019-10-03 04:35:27

标签: javascript html

如果文本框为空,则返回true
否则,显示确认

工作测试:http://next.plnkr.co/edit/KfD3oznkl3eBXgPx

我正在尝试做的事情:

当用户输入尝试单击“提交”并且密码文本字段为空白时,则应直接返回true,但是如果密码文本字段中包含数据,则应显示确认提示。

我做了什么:

当密码文本字段为空时,它直接返回true,但是当有值时,它显示确认,但是即使我在确认框中单击“否”,它也会返回true

代码:

<script>
function validateForm(e) {
  var password=document.getElementById("password").value;
  if (password==""){
    return true;
  }
  else{
    confirm("Are You Sure you wanted to change password?");
  }
}
</script>
<h1> Edit Profile </h1>
<form onSubmit="return validateForm()">
  <input type="text" placeholder="Username" id="uname"><br>
  <input type="password" placeholder="password" id="password"><br>
  <input type="text" placeholder="email" id="email"><br>
  <input type="submit" id="check" value="OK">
</form>

6 个答案:

答案 0 :(得分:4)

Window.confirm()

  

返回一个布尔值,指示是选择了确定(true)还是取消(false)。如果浏览器忽略页面内对话框,则结果始终为假。

您应检查confirm返回的值

function validateForm(e) {
  var password=document.getElementById("password").value;
  if (password==""){
    return true;
  }
  else{
    var confirmRes = confirm("Are You Sure you wanted to change password?");
    return confirmRes;
    //OR Simply
    //return confirm("Are You Sure you wanted to change password?");
  }
}
<h1> Edit Profile </h1>
<form onSubmit="return validateForm()">
  <input type="text" placeholder="Username" id="uname"><br>
  <input type="password" placeholder="password" id="password"><br>
  <input type="text" placeholder="email" id="email"><br>
  <input type="submit" id="check" value="OK">
</form>

答案 1 :(得分:2)

confirm返回您在程序中未使用的布尔值。 尝试在确认之前添加return,以实际使用确认函数提供的值

if (password==""){
    return true;
}
else{
  return confirm("Are You Sure you wanted to change password?");
}

希望这会有所帮助:)

答案 2 :(得分:0)

confirm API将根据用户选择返回truefalse。因此,您可以按如下所示从confirm返回值

function validateForm(e) {
   var password = document.getElementById("password").value;
   return password === "" || confirm("Are You Sure you wanted to change password?");
}

答案 3 :(得分:0)

我已经编辑了您的代码,看它是否有效:

function validateForm(e) {
          var password=document.getElementById("password").value;
          if (password==""){
              document.getElementById("myform").submit();
          }
          else{
            if(confirm("Are You Sure you wanted to change password?")){
              document.getElementById("myform").submit();
            }
          }
      }
<h1> Edit Profile </h1>
    <form id="myform">
      <input type="text" placeholder="Username" value="Tommy" id="uname"><br>
      <input type="password" placeholder="password" id="password"><br>
      <input type="text" placeholder="email" value="test@gmail.com" id="email"><br>
      <input type="button" id="check" value="OK" onclick="validateForm()">
    </form>

答案 4 :(得分:0)

当密码为空时,您必须返回false而不是true。那就是当您没有输入密码时,您不必提交表单。还需要输入return语句以进行确认。进行如下更改。

function validateForm(e) {
    var password=document.getElementById("password").value;
    if (password==""){
      return false;
    }
    else{
      return confirm("Are You Sure you wanted to change password?");
    }
  }

答案 5 :(得分:0)

尝试一下,我在else部分中检查了确认值。

      function validateForm(e) {
      
          var password=document.getElementById("password").value;
          if (password==""){
              return true;
          }
          else{
            var r = confirm("Are You Sure you wanted to change password?");
            if (r == true){
              return true;
            }
            else {
              return false;
            }
          }
      }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1> Edit Profile </h1>
    <form onSubmit="return validateForm()">
      <input type="text" placeholder="Username" value="Tommy" id="uname"><br>
      <input type="password" placeholder="password" id="password"><br>
      <input type="text" placeholder="email" value="test@gmail.com" id="email"><br>
      <input type="submit" id="check" value="OK">
    </form>