Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  179] [ 5]  / answers: 1 / hits: 101587  / 9 Years ago, thu, june 11, 2015, 12:00:00

The code below is supposed to validate the username and password and if correct should run run.html else run fail.html,But the code is not executing as required.What is the error in the code?



<!doctype html>
<html>
<body>

<form>

<font size=5><font color=black><input type=text placeholder=Username name=text1><br /><br /></font>

<font size=5><font color=white><input type=password placeholder=Password name=text2><br /><br /></font>

<font size=5><input type=submit value=Login onclick=javascript.validate(test1.value,test2.value)></font>

</form>

<! javascript>
<script language=javascript>
function validate(text1,text2)
{
if(text1==workshop && text2==workshop)
load(run.html);
else
{
load(fail.html);
}
}
function load(url)
{
location href=url;
}
</script>
</body>
</html>

More From » html

 Answers
17

Firstly - don't validate credentials like this - do it on the server side as anything client side is practically worthless for security.



Other than that...




  • Use css to change the presentation of elements rather than html elements like font.

  • javascript.validate(...) should be javascript:validate(...) or just validate(...).

  • Use <script type=text/javascript> to start a (java)script element.

  • location href should be location.href

  • You need to wrap string values in quotes (i.e. workshop should be workshop).

  • Change name=text1 to id=text1 and then you can get the value using document.getElementById(text1).value.





<form>
<input type=text placeholder=Username id=text1 /><br />
<input type=password placeholder=Password id=text2 /><br />
<input type=button value=Login onclick=javascript:validate() />
</form>
<script type=text/javascript>
function validate()
{
if( document.getElementById(text1).value == workshop
&& document.getElementById(text2).value == workshop )
{
alert( validation succeeded );
location.href=run.html;
}
else
{
alert( validation failed );
location.href=fail.html;
}
}
</script>




[#66237] Tuesday, June 9, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cartersinceren

Total Points: 442
Total Questions: 116
Total Answers: 106

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
cartersinceren questions
Thu, Nov 5, 20, 00:00, 4 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Fri, Feb 1, 19, 00:00, 5 Years ago
;