仅在验证表单时打开模式

问题描述 投票:0回答:2

我想仅在表单完全验证无错误时打开模式对话框。首先,我禁用了按钮,以便它首先验证,并且仅在验证表单时才启用。我正在使用 jquery 验证,但没有得到我想要的结果。我希望当用户单击提交按钮时,如果字段未填写,则不应打开模式。 这是我的 HTML 代码

                      <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-white" type="reset">Cancel</button>
                                     <!-- Trigger the modal with a button -->
                                    <button type="button" id="myBtn" class="btn btn-primary" data-toggle="modal" data-target="#myModal"  disabled />Confirm</button> 
                                </div>
                      </div>

还有我的 jquery

(function() {
$('#PasswordForm > input').keyup(function() {

    var empty = false;
    $('#PasswordForm > input').each(function() {
        if ($(this).val() == '') {
            empty = true;
        }
    });

    if (empty) {
        $('#myBtn').attr('disabled', 'disabled'); 
    } else {
        $('#myBtn').removeAttr('disabled'); 
    }
});})()

我使用的表单验证是

$(document).ready(function() 
{
    $('#PasswordForm').formValidation({
        message: 'This value is not valid',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: 
		{
            NPassword: 
			{
                validators: 
				{
                    notEmpty: 
					{
                        message: 'The password is required and can\'t be empty'
                    }
                }
            },
            RPassword: 
			{
                validators: 
				{
                    notEmpty: 
					{
                        message: 'The confirm password is required and can\'t be empty'
                    },
                    identical: 
					{
                        field: 'NPassword',
                        message: 'The password and its confirm are not the same'
                    }
                }
            }
        }
    });
});
<form id="PasswordForm" method="post" class="form-horizontal" action="/HRM/HRM.PasswordChange">
							    <div class="hr-line-dashed"></div>
                                <div class="form-group">
								<label class="col-sm-2 control-label">Employee Name</label>
                                    <div class="col-sm-10">
										<input type="text" disabled="" value="@@UserName@@" class="form-control">
									</div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
									<label class="col-sm-2 control-label">New Password</label>
                                    <div class="col-sm-10">
                                        <div class="row">
                                            <div class="col-md-6">
												<input type="password"  name="NPassword" placeholder="New Password" class="form-control">
											</div>
                                        </div>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
								<div class="form-group">
									<label class="col-sm-2 control-label">Retype Password</label>
                                    <div class="col-sm-10">
                                        <div class="row">
                                            <div class="col-md-6">
												<input type="password" name="RPassword" placeholder="Retype Password" class="form-control">
											</div>
                                        </div>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-2">
                                        <button class="btn btn-white" type="reset">Cancel</button>
										 <!-- Trigger the modal with a button -->
										<button type="button" id="myBtn" class="btn btn-primary" data-toggle="modal" data-target="#myModal"  disabled />Confirm</button> 
                                    </div>
                                </div>
                            </form>

如有任何建议或帮助,我们将不胜感激。

javascript jquery html validation twitter-bootstrap-3
2个回答
2
投票

尝试将按钮更改为:

<button type="button" id="myBtn" class="btn ban-primary">Confirm</button>

并添加以下功能:

$('#myBtn').on('click', function(){
  if(!empty) {
   $('#myModal').modal('toggle');
  }
});

您必须将全局变量设置为空。 (未经测试的代码)


0
投票
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Validation Example</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<!-- Your form -->
<form id="emailForm">
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" required>
    <button type="button" onclick="validateEmail()">Submit</button>
</form>

<!-- Bootstrap Success Modal -->
<div class="modal fade" id="successModal" tabindex="-1" role="dialog" aria-labelledby="successModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="successModalLabel">Success!</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        Email is valid.
      </div>
    </div>
  </div>
</div>

<!-- Bootstrap Error Modal -->
<div class="modal fade" id="errorModal" tabindex="-1" role="dialog" aria-labelledby="errorModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="errorModalLabel">Error!</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        Please enter a valid email.
      </div>
    </div>
  </div>
</div>

<!-- Bootstrap JS and Popper.js -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<!-- Your JavaScript -->
<script>
    // Function to validate email
    function validateEmail() {
        var emailInput = document.getElementById('email');
        var email = emailInput.value;

        // Basic email validation, you can replace this with your own validation logic
        var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (emailRegex.test(email)) {
            // Show success modal
            $('#successModal').modal('show');
        } else {
            // Show error modal
            $('#errorModal').modal('show');
        }
    }
</script>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.