表单验证检查后不显示模式框



我有一个注册表,在这个表单中,我required对一些输入字段进行验证。验证检查后,我想打开一个模式框$('#exampleModal').modal('show');但它不起作用

法典:

<form class="well form-horizontal"   id="contact_form" autocomplete="off">
<fieldset>
<legend><center><h2><b>Registration Form</b></h2></center></legend><br>
<div class="form-group">
<label class="col-md-4 pd-r control-label">firstname</label>  
<div class="col-md-4 pd-r inputGroupContainer">
<div class="input-group">
<input  name="first_name" id="first_name"  class="form-control tboxs"  type="text" minlength="4" required>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 pd-r control-label">village</label> 
<div class="col-md-4 pd-r inputGroupContainer">
<div class="input-group">
<input name="village" id="village" class="form-control tboxs" type="text" required>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 pd-r control-label"></label>
<div class="col-md-4 pd-r"><br>
<button type="submit" value="submit" class="btn-theme-colored btn">SUBMIT <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</fieldset>
</form>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><font color="black"><strong>Verify OTP</strong></h5>
</div>
<div class="modal-body">
<form method="POST"  id="otp_form" action="">
<div class="row">
<div class="col-sm-12 form-group">
<input type="text" class="form-control tboxs" id="otp_data" name="otp_data" placeholder="Enter OTP" maxlength="4" required="">
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<button type="submit" name="verifyotp" class="btn btn-lg btn-info btn-block">Verify</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>

脚本:

<script type="text/javascript">
$('#submit').click(function(){
if($('#first_name').val()==''){
alert('Name can not be left blank and atleast 4 char long');
return false;
}
if($('#village').val() == ''){
alert('village can not be left blank');
return false;
}
$('#exampleModal').modal('show');
return true;
});
</script>

在代码中,验证在验证复选框模式框未打开后工作。我不知道我在代码中哪里错了。

这是您更新的scripthtml代码

HTML 代码问题:

  • 按钮类型已submit,但您正在对其调用click事件
  • 您没有将id分配给特定的按钮eg: id='submit'

$('#submit').click(function(){
if($('#first_name').val() == '' ){
alert('Name can not be left blank and atleast 4 char long');
return false;
}
if($('#village').val( ) == '') {
alert('village can not be left blank');
return false;
}
$('#exampleModal').modal('show');
return true;

});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<form class="well form-horizontal" id="contact_form" autocomplete="off">
<fieldset>
<legend><center><h2><b>Registration Form</b></h2></center></legend><br>
<div class="form-group">
<label class="col-md-4 pd-r control-label">firstname</label>  
<div class="col-md-4 pd-r inputGroupContainer">
<div class="input-group">
<input  name="first_name" id="first_name"  class="form-control tboxs"  type="text" minlength="4">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 pd-r control-label">village</label> 
<div class="col-md-4 pd-r inputGroupContainer">
<div class="input-group">
<input name="village" id="village" class="form-control tboxs" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 pd-r control-label"></label>
<div class="col-md-4 pd-r"><br>
<button type="button"  id="submit" value="submit" class="btn-theme-colored btn">SUBMIT <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</fieldset>
</form>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><font color="black"><strong>Verify OTP</strong></h5>
</div>
<div class="modal-body">
<form method="POST"  id="otp_form" action="">
<div class="row">
<div class="col-sm-12 form-group">
<input type="text" class="form-control tboxs" id="otp_data" name="otp_data" placeholder="Enter OTP" maxlength="4" required="">
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<button type="submit" name="verifyotp" class="btn btn-lg btn-info btn-block">Verify</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>

相关内容

  • 没有找到相关文章

最新更新