I am building a registration form and I found out that the Safari browser doesn't work with the HTML5 'Required' tag. So I built this Javascript script to ensure that users MUST submit data or the form will not submit.
However, if you enter a username and password, but no email address, the form still submits. (The form doesn't submit if you leave out username and/or password).
So the problem seems to be with the email address part of the script. I don't want the form to submit if the email field is blank.
Here is my code:
<script>
function ValidateLoginForm()
{
var username = document.form1.username;
var password = document.form1.password;
var email = document.form1.email;
if (username.value == )
{
alert(Your username wasn't recognised.);
username.focus();
return false;
}
if (password.value == )
{
alert(Please enter a password.);
password.focus();
return false;
}
if (email.value == )
{
alert(Please enter your email address.);
mypassword.focus();
return false;
}
else{
return true;
}
}
</script>
Here is my form (inside a table)
<table width=300 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<form name=form1 method=post action=login/process.php onsubmit=return ValidateLoginForm();>
<td>
<table width=100% border=0 cellpadding=3 cellspacing=1 bgcolor=#FFFFFF>
<tr>
<td colspan=3><center><strong>Registration Form</strong></center></td>
</tr>
<tr>
<td width=78>Username</td>
<td width=6>:</td>
<td width=294><input name=username type=text id=username required></td>
</tr>
<tr>
<td>Password*</td>
<td>:</td>
<td><input name=password type=text id=password pattern=.{5,} title=Minimum length of 5 letters or numbers. required></td>
</tr>
<tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name=email type=text id=email required></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type=submit name=submit value=Submit Registration Form></td>
<p>*Password must consist of numbers and letters.. 5 Characters minimum.</p>
</tr>
</table>
</td>
</form>
</tr>
</table>
Thank you