Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  193] [ 2]  / answers: 1 / hits: 21015  / 11 Years ago, sat, august 10, 2013, 12:00:00

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=commit 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 erron351 ! ')
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..


More From » html

 Answers
56

Since 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=commit 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).


[#76418] Thursday, August 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
louiemarvinm

Total Points: 473
Total Questions: 103
Total Answers: 94

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
;