javascript - 通过检查文本框和复选框进行验证,进入下一个表单

标签 javascript html validation bootstrap-modal

我得到了一个带有 2 个文本框和 1 个复选框的表单。我在验证表单时遇到问题。我的目标是检查用户是否选中了复选框或在文本框中写入了内容。如果用户在文本框中写入或选中复选框,那么他们可以继续下一个表单。如果没有,则应出现一个对话框。我该如何实现这个目标?

enter image description here

我的伪代码:

if (checkBox1.Checked)
{
    //Go to next Form
}
else
{
    if (textBox1.Text == "" || textBox2.Text == "")
    {
        MessageBox.Show("Please fill in the textboxes or mark the checkbox to continue");
    }
    else
    {
        //Go to next Form
    }
}

我的 HTML 表单

<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
    </script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </script>

    <script>
    jQuery(document).ready(function()
    {
        jQuery('#continue').on('click', function()
        {
            jQuery('#stepTwoForm').submit();
            return false;
        })

    })
    </script>


</head>

<body>
        <form id="stepTwoForm" action="NextForm.php" method="POST" class="form-horizontal">
              <div class="form-group">
                <label for="Textbox1" class="col-sm-3 control-label">Textbox1</label>
                <div class="col-sm-6">
                  <input name="Textbox1" type="text" class="form-control" id="Textbox1">
                </div>
              </div>
              <div class="form-group">
                <label for="Textbox2" class="col-sm-3 control-label">Textbox2</label>
                <div class="col-sm-6">
                  <input name="Textbox2" type="text" class="form-control" id="Textbox2">
                </div>
              </div>

          <center><input type="checkbox" name="checkboxName" id="checkboxID" value="Bike"> If checked go to next page or else fill out the textboxes</center>
        </form>

    <center><a id="continue" type="button" class="btn btn-primary btn-md" href="#">Continue</a></center>

</body>
</html>

最佳答案

好的,如果您在提交之前使用 jquery 验证表单,那么您可以通知用户错误。看一下下面的代码,这应该会让您朝着正确的方向前进,如果未选中复选框并且输入为空,它将向用户发出警报,否则它将提交表单。

<script>
  $(document).ready(function(){
    $( '#continue' ).on( 'click', function()
    {
        if( $( '#checkboxID' ).is( ':checked' ) )
      {
        $( '#stepTwoForm' ).submit();
      }
      else
      {
        if( $( 'input[name=Textbox1]' ).val() == '' || $( 'input[name=Textbox2]' ).val() == '' )
        {
          alert( 'Please fill in the textboxes or mark the checkbox to continue' );
        }
        else
        {
          $( '#stepTwoForm' ).submit();
        }
      }
    })
  })
</script>

工作示例 - https://jsfiddle.net/Lhuaz0fn/

关于javascript - 通过检查文本框和复选框进行验证,进入下一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093400/

相关文章:

Javascript "' + aStringVarible + '"(双引号加单引号)有什么作用?

javascript - 在这种情况下如何使用 ng-mouseenter 删除类

javascript - socket.io 在点击后显示多个用户数据

ruby-on-rails - 序列化数组的条件验证

c# - 具有多个参数的自定义验证属性

matlab - 如何将函数句柄验证为输入参数?

Javascript 运算符无法将整数与零进行比较(例如 10、100、1000 等)?

php - 如何绕过 toHtml() 在 Magento 中打印 html 标签

javascript - 没有 html 元素的 ng-repeat

Html 文件未插入 Mysql 表中