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

108
Visualizações
Javascript function getting cutoff?

I'm using MVC to build a login page. I have a Javascript function that gets called when the submit button is clicked. However I noticed in the debugger that the function doesnt get executed fully. Here is my code:

@{
    string loginError = (string)ViewData["loginError"];
}


<script type="text/javascript">
    function checkLogin() {
        if (@loginError != null)
        {
            var message = '@loginError';
            if (message)
            alert(message);
        }
    }
</script>


<form class="login-form" action="/Login/Login" method="POST">

    <div class="login-form__content">

        <div class="login-form__header">Login to your account</div>

        <input class="login-form__input" type="text" name="username" placeholder="Username">

        <input class="login-form__input" type="password" name="password" placeholder="Password">

        <button class="login-form__button" type="submit" onsubmit="checkLogin();">Login</button>

        <p></p>

        <button class="login-form__button" type="submit" formaction="/Register/Create" id="signupbutton">Sign up</button>

    </div>

</form>

So, assuming that a user enters the wrong credentials, the loginError variable will be assign a value "Invalid username or password". Oddly after the line: var message = '@loginError', the function just stops there.

Is there something wrong with my code?

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

0

Try this:

function checkLogin() {
    if (@loginError != null)
    {
        var message = '@loginError';
        if (message)
            alert(message);
    }
}

What I think happened was that the program stopped because you forgot to indent the alert, and the program ran alert(message) every time, even without the if statement.

This was the first problem I saw, so this maybe isn't the problem.

about 4 years ago · Juan Pablo Isaza Relatório

0

checklogin reloads onsubmit and breaks in the middle of the execution of javascript. make sure you put return false; onsubmit in order to execute javascript and to disable reload.

you can see example here: HTML form with custom function call

about 4 years ago · Juan Pablo Isaza Relatório

0

checklogin reloads onsubmit and breaks in the middle of the execution of javascript. make sure you put return false; onsubmit in order to execute javascript and to disable reload.

you can see example here HTML form with custom function call

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