Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  184] [ 4]  / answers: 1 / hits: 18361  / 12 Years ago, sat, november 24, 2012, 12:00:00

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>&nbsp;</td>
<td>&nbsp;</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


More From » html

 Answers
3

It looks like you are focusing a non-existent item mypassword, which causes your script to terminate in error and the form to submit normally.



if (email.value == )
{
alert(Please enter your email address.);
// Focus the email rather than mypassword (which doesn't exist)
email.focus();
return false;
}
// All's well if you got this far, return true
return true;


Here is the working code



It is important to always develop JavaScript with your browser's error console open. You would have seen an error about an undefined object mypassword, which could point you to the faulty line in your code.


[#81824] Thursday, November 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
seth

Total Points: 307
Total Questions: 114
Total Answers: 96

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
;