Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
prompt window submits form no matter the if else check

I have integrated confirmation window to my Symfony form with javascript function.

This is my start form definition:

{{ form_start(form, {'attr': {'role': 'form', 'id':'editForm'} } ) }}

And in javascript:

$('#editForm').on('submit', function (e) {
e.preventDefault();
validateEditForm();
});

function validateEditForm()
{
    var nameInput;
    nameInput = prompt('Please input name to confirm edit:');
    var name = document.getElementById('edit_name').value;

if (nameInput === name || Number(nameInput) === name) {
    $('#editForm').submit();
} else if (nameInput === null || nameInput === "") {
    alert('You must enter name to confirm!');
    return false;
} else {
    alert("Entered name and default name don't match!");
    return false;
}

One case is not working.

When I enter correct name in the box then Please input workspace name to confirm edit: appears again. Then I click OK again and the form is submitted.. Is there something wrong with my if else checks?

Edit: Tried to do it in two separate functions and used e.preventDefault(); and the behaviour was the same.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I change validateEditForm method as returning boolean. On the form submit event, validateEditForm method decides whether submit the form or not.

$('#editForm').on('submit', function (e) {

    if(!validateEditForm()) {
         e.preventDefault();
    }
});

function validateEditForm()
{
    var nameInput;
    nameInput = prompt('Please input name to confirm edit:');
    var name = document.getElementById('edit_name').value;

if (nameInput === name || Number(nameInput) === name) {
    return true;
} else if (nameInput === null || nameInput === "") {
    alert('You must enter name to confirm!');
    return false;
} else {
    alert("Entered name and default name don't match!");
    return false;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda