Steve
Steve

Reputation: 61

Javascript - JSP page won't run .js file

I have javascript code in a .js file which validates the text fields but it won't work when it's an external file. I mean when the code is separated from the JSP.

<script type="text/javascript"> 

function checkForm(form) 
{ 
    if(form.username.value == "") 
    { 
            alert("Error: Username cannot be blank!"); 
            form.username.focus(); return false; 
    }
}

</script> 

JSP

<HEAD><script type="text/javascript" src="check.js"></script></HEAD>

<form name="register"  method="post" action="registerUser.jsp" onsubmit="return (checkForm(this) && false);">

<table summary="Register Form" cellspacing="15">
<tr>
<td align="right"> Username: </td> <td><input name="username" size=20 type="text"/></td> <td>*Min: 4 chars.</td>
</tr>

</table>

<div id="Buttons">
<p><input type="submit" value="Register">
<input type="reset" value="Reset Fields"></p>
</div>

</form>

Upvotes: 1

Views: 2059

Answers (3)

risico
risico

Reputation: 36

It works ok for me. I know it sounds dumb, but did you removed the tags from the check.js file?

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691685

The js file should only contains javascript code. The <script type="text/javascript"> line and the </script> line contain HTML code, not JavaScript. Remove them from the .js file.

Upvotes: 1

Pekka
Pekka

Reputation: 449395

You need to remove the tags

<script type="text/javascript"> 
</script>

from the external file.

Upvotes: 3

Related Questions