Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!