Problems with the confirm function in javascript help me please

I will explain myself better. I have this function.,works with a form called fm1, what happens if the image is not loaded, it asks if you want to send the image and sends it with ok, but if the user wants to update the image, press calcel, the question is asked if the image is empty and the user loads it, if the image is not jpg the alert should come out that the image is not jpg for the user to change it but but but but the question appears if you want to upload the image if you upload it this is the first error then when changing the image to a jpg, and submitting the form again it exits; want to upload the image if upload the same second error

I can’t resolve that it only asks once if you want to upload the image without uploading, it is nesting the if sequence.

Can someone help me or tell me what’s going on, it’s not the keyboard, it’s the function, there will be another way to do it, and excuse my English, thanks

<script>
function Entrar(){	 

// Validar Direccion Habitacion
if(document.fm1.direccion.value==""){
alert("El Campo Direccion de habitación esta Vacio");
return false;
}

  if (confirm("Foto No Actualizada, se subira sin actualizar")) {
 document.fm1.submit();   
 return false; 
 
} 

//Validar Selección de Imagen
if(document.fm1.imagen.value==""){
alert("Debe de subir un Archivo de Foto Ext: JPG");
return false;
}

//Validar la Extensión del archiqie que debe ser JPG

var expr=/\.jpg|jpeg $/i;
if(!expr.test(document.fm1.imagen.value)){
alert("Error: la Extension de la Foto debe ser JPG");
return false;
}

//Validar el tamaño de la Imagen
var expr=document.fm1.imagen.files[0].size;
if(expr>1000000){
alert("Debe de subir un Archivo NO mayor a 1MB (MegaByte)");
return false;
}

document.fm1.submit();
}
 
</script>

</head>
<body>

<!-- Menú -->

<?php

 include 'menu.html';

?>


<!-- Sección del Body y Footer -->

<!-- Linea 1 del Formulario -->

<div class="container mt-3">
  
      <form action="regcolaborador.php"  method="post" name="fm1" enctype="multipart/form-data" >
 
        <button type="button" class="btn btn-primary"  value="Entrar"   onClick="Entrar()"  >Enviar</button>
        <button type="reset" class="btn btn-primary">Borrar</button> 



  </form>

Does this do what you want?

//Validar Selección de Imagen
if(document.fm1.imagen.value==""){
  if( confirm("¿Quieres subir un formulario sin foto?") ) {
    document.fm1.submit();
    return;
  }
  else {
    alert("Debe de subir un Archivo de Foto Ext: JPG");
    return false;
  }
}

(I used Google to translate confirm message :grinning:)

I SOLVED IT LIKE THIS AND THEN ASK THE PHP TO UPLOAD A PHOTO AND SOME OTHER INPUT THAT SAVE IT IN THE BD WITH AN UPDATE IF ELSE, IF YOU UPLOAD ONLY THE PHOTO FILE THAT SAVE ONLY THE PHOTO WITH THE IF ELSE, I WILL CHECK YOUR CODE AND NOTIFY YOU THANK YOU FOR YOUR TIME AND PATIENCE Archibald

if(document.fm1.imagen.value!=“”){

//Validar la Extensión del archiqie que debe ser JPG

var expr=/.jpg|jpeg $/i;
if(!expr.test(document.fm1.imagen.value)){
alert(“Error: la Extension de la Foto debe ser JPG”);
return false;
}

//Validar el tamaño de la Imagen
var expr=document.fm1.imagen.files[0].size;
if(expr>1000000){
alert(“Debe de subir un Archivo NO mayor a 1MB (MegaByte)”);
return false;

}
}

document.fm1.submit();
}

Your code is perfect thanks friend excellent another way

You are great among the great… Archibald

I am pleased my suggested code was helpful.

I think you do not need return false . . . . just use return.

You would need return false if you are listening for a form submit event like this (or by using addEventListener to listen for submit event).

Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.