Am new to javascript. Am trying to call an external javascript file from jsp.
I have no problem in doing the above but when I tried to place this javascript in a different folder and then call, am not able to do it.
Code which calls js from jsp
<input type="submit" name="submit" value="Submit" onclick="validate(this.form)"></input>
Added these lines in the head part of the jsp
<script src="scripts/validate.js">
</script>
Structure: ServletsApp is the main project folder in which WebContent is present. WebContent has folders views and scripts. My jsp resides in views and .js in scripts folder.
Update: LoginExample.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Share Market Application</title>
<script src="../WebContent/scripts/validate.js">
</script>
</head>
<body>
<form name="loginForm">
<h2>Enter user credentials</h2>
<br>
<font size="3" style="TimesNewRoman">Username:</font>
<input type="text" name="userId"></input>
<br>
<font size="3" style="TimesNewRoman">Password:</font>
<input type="password" name="password"></input>
<br>
<input type="submit" name="submit" value="Submit" onclick="validate(this.form)"></input>
<input type="button" name="cancel" value="Cancel"></input>
</form>
Validate.js
function validate(form)
{
alert("Entered");
alert(document.getElementById("userId"));
var userName=form.userId.value;
alert(userName);
var pwd=form.password.value;
alert(document.getElementById("userId").valueOf());
if(userName=="" || userName!="admin")
{
alert("User name is incorrect");
}
else
{
if(pwd=="" || pwd!="admin")
{
alert("Password is incorrect");
}
}
}