I have two files. One is login.html
which is a simple html5 file with a form.
HTML
<form method="post" action="" name="form1">entre the pasword:
<input type="password" name="code" placeholder="code" maxlength="6">
<p class="submit">
<input type="submit" name="mit" value="send" onclick="verif(document.form1.code)">
</p>
</form>
Second is my javascript file with the below code:
function verif(inputtxt) {
var pwd = "123456";
if (inputtxt.value.match(pwd)) {
window.location.href = 'Test.html';
} else {
alert('Code erron\351 ! ')
return false;
}
}
Now my problem is that when I enter my password, if it is wrong the alert message indicating an error should appear (it appears and I don't have a problem with that) and if it is correct, I should get redirected to the next page. The second part doesn't work for me.
Please help, I'm stuck with that for two days now..
I have two files. One is login.html
which is a simple html5 file with a form.
HTML
<form method="post" action="" name="form1">entre the pasword:
<input type="password" name="code" placeholder="code" maxlength="6">
<p class="submit">
<input type="submit" name="mit" value="send" onclick="verif(document.form1.code)">
</p>
</form>
Second is my javascript file with the below code:
function verif(inputtxt) {
var pwd = "123456";
if (inputtxt.value.match(pwd)) {
window.location.href = 'Test.html';
} else {
alert('Code erron\351 ! ')
return false;
}
}
Now my problem is that when I enter my password, if it is wrong the alert message indicating an error should appear (it appears and I don't have a problem with that) and if it is correct, I should get redirected to the next page. The second part doesn't work for me.
Please help, I'm stuck with that for two days now..
Share Improve this question edited Aug 10, 2013 at 3:08 Harry 89.8k26 gold badges212 silver badges222 bronze badges asked Aug 10, 2013 at 2:47 neustreneustre 1071 gold badge2 silver badges14 bronze badges 6- What exactly doesn't work? Be specific. – user1508519 Commented Aug 10, 2013 at 2:49
- i've mentioned that the redirection doesn't work ... – neustre Commented Aug 10, 2013 at 2:51
- if (inputtxt.value.match(pwd)) { window.location.href = 'Test.html' ;} the first test doesn't send me to the page i want – neustre Commented Aug 10, 2013 at 2:52
-
3
Does it enter the if? And did you try simply
inputtxt.value == pwd
? For the record, this is a password protection method you should NEVER use, except for studying. – bfavaretto Commented Aug 10, 2013 at 2:53 -
put
alert('redirect');
right beforewindow.location.href = 'Test.html' ;}
to see if you're hitting the if block, and let us know.. – asifrc Commented Aug 10, 2013 at 2:55
1 Answer
Reset to default 3Since your button is a submit button, I think it is submitting the form after the JS is done and this could be the reason why you don't get redirected to Test.html (as form action
attribute doesn't have any value.) Try the below code for the HTML form and check if this solves the issue.
<form method="post" action="" name="form1" onsubmit="verif(document.form1.code);return false;">entre the pasword:
<input type="password" name="code" placeholder="code" maxlength="6">
<p class="submit">
<input type="submit" name="mit" value="send">
</p>
</form>
The return false;
in the onsubmit
attribute prevents the form's default submit action. The verif(document.form1.code)
will be executed whenever the form is submitted (that is the submit button is clicked).