Without a "name" for your form, that code won't work. I would use a name for your form that is identical to the form's id.
Also, in general, you should avoid using an "id" or "name" that also refers to an element or attribute in the DOM.
So, putting the two ideas together, try something like this:
In your HTML:
In your JS:
<form id="form1" name="form1" method="post" action="file.php">
<p>Username: <input type="text" name="username" /></p>
if (document.form1.username.value == '')