Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda