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?
CodePudding user response:
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.
CodePudding user response:
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
CodePudding user response:
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