Javascript input check

Asked

Viewed 108 times

0

I am making a form with sending 2 types of photo, and I would like to know how to do a javascript check to see if the person sent any of the 2 options, and if you have not sent any of the 2 does not proceed to next page...

	<div class="fileUpload btn">
	<div>

		<span style="width:250px">
		<img src="images/enviar1.png" width="235" height=""/></span></div>
       
	    <input type="file" name="img1"  id="img1" accept="image/*" capture="camera" onchange="" class="upload" />
                </div>
                	<div class="fileUpload btn" >
        	<div>
        		<span style="width:250px">
        	<img src="images/enviar2.png" width="235" height=""/></span></div>

	    <input type="file" name="img2"  id="img2" accept="image/*" capture="camera" onchange="" class="upload" />

    
</div>

<input style="margin-left:25px;" type="submit" name="botao" id="botao" value="CONTINUAR"/>

3 answers

2


It is possible to use the function submit of our jquery friend, when sending the form , a check is made on function call.

What was done in the code below, was to check if at least one image was selected in one of the two inputs, if it exists it continues and sends the form, otherwise closes the application and shows an alert.

// Executa ao enviar o formulário, <form>
$('form').submit(function(){
// Verifica se não foi selecionado nada nos dois inputs
if($('#img1').val() === "" && $('#img2').val() === ""){
  alert('selecione uma imagem');
return false
}

alert('enviado');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<form>
<div class="fileUpload btn">
	<div>
    <span style="width:250px">
		<img src="images/enviar1.png" width="235" height=""/></span>     </div>
  <input type="file" name="img1"  id="img1" accept="image/*" capture="camera" onchange="" class="upload" />
</div>
<div class="fileUpload btn" >
  <div>
    <span style="width:250px">
    <img src="images/enviar2.png" width="235" height=""/></span>
  </div>
  <input type="file" name="img2"  id="img2" accept="image/*" capture="camera" onchange="" class="upload" />
</div>
<input style="margin-left:25px;" type="submit" name="botao" id="botao" value="CONTINUAR"/>
</form>


Reference: Submit - Jquery

  • He’s going straight to the Sent alert and going to the next page !

  • I changed the code to stop the operation if sent.

  • Not like, I just want you to interrupt if the person doesn’t upload, if you do, click and go to the next page understood?

  • Yes, but that’s what the code does, it stops only if there is nothing selected in the inputs.

  • It is not working, here in the test of the site itself it appears the 2 alerts if you do not send anything..

  • 1

    I removed the return false from the wrong place, ready!

  • Guy worked, thank you very much ! if I’m going to do just one I remove && $('#img2'). val() === "") ??

  • 1

    This, in this case it checks whether the two elements are empty, in case wanted to check only one would be so even, if($('#img1').val() === "")

  • Thanks a lot, thanks a lot !

Show 4 more comments

0

use the plugin jquery validation

it is a complete validation library for forms, very good and simple to install and use.

$(seletorDoFormulario).valid()

this example above will return you a boolean thus facilitating life to know if the form is valid to be sent.

-1

In your case, as you only need to know if he sent at least one of the two, the attribute required HTML in the first input would not be enough?


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<form>
<div class="fileUpload btn">
    <div>
    <span style="width:250px">
        <img src="images/enviar1.png" width="235" height=""/></span>     </div>
  <input type="file" name="img1"  id="img1" accept="image/*" capture="camera" onchange="" class="upload" required />
</div>
<div class="fileUpload btn" >
  <div>
    <span style="width:250px">
    <img src="images/enviar2.png" width="235" height=""/></span>
  </div>
  <input type="file" name="img2"  id="img2" accept="image/*" capture="camera" onchange="" class="upload" />
</div>
<input style="margin-left:25px;" type="submit" name="botao" id="botao" value="CONTINUAR"/>
</form>


  • required will force you to always mark option 1, and according to the author you can choose to mark 1 or 2, provided you check at least some option and not always the first

Browser other questions tagged

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