Form with mandatory input sends empty POST - Bootstrap

Asked

Viewed 132 times

0

The form within a modal is sending the POST even with the required input but not completed. The modal is inside a separate file from the page and is included by PHP include().

When executing the snippet the validation works, if it is empty does not send, but on the site does not work, which can be?

$(document).ready(function(){
  $('#modalFormAnunciar').modal('show');
	$("#formCampoUnico").on('submit', function(e) {
		e.preventDefault();
		parent.location.href='404.php';
	});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div id="modalFormAnunciar" class="modal fade" role="dialog">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal">&times;</button>
				<h6 class="modal-title">Anunciar Premium</h6>
			</div>
			<div class="modal-body">
				
<form role="form" id="formCampoUnico" class="formCampoUnico" name="formCampoUnico">
	<div class="col-sm-12">
		<div class="form-group">
			<label for="razaoSocial" class="font-size-12">Razão Social: <span class="input-required">*</span></label>
			<input name="razaoSocial" id="razaoSocial" type="text" class="form-control" required="required" placeholder="Razão Social do CNPJ" autofocus>
		</div>
	</div>
	<div class="col-sm-12 return"></div>
  <div class="col-sm-12">
      <button id="btnSend" type="submit" class="btn btn-black font-size-14">Enviar</button>
  </div>
</form>

            <div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
			</div>
		</div>
	</div>
</div>

  • Take a look at the "inspect element" and see if the required all right.

  • @Sam is, and when clicking the Submit button, the message appears to fill the field, but the form is sent anyway.

  • I found the error, there was another form that took only the event page click, another instance, and ran the form above, my blunder!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.