我正在构建一个注册表单,我发现Safari浏览器无法使用HTML5“必需”标记。所以我构建了这个Javascript脚本以确保用户必须提交数据,否则表单将不会提交。
但是,如果您输入用户名和密码,但没有电子邮件地址,则表单仍会提交。 (如果您遗漏用户名和/或密码,表格不会提交。)
所以问题似乎与脚本的电子邮件地址部分有关。如果电子邮件字段为空,我不希望提交表单。
这是我的代码:
<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>
这是我的表格(在表格内)
<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>
谢谢
答案 0 :(得分:1)
看起来你正在关注一个不存在的项mypassword
,这会导致你的脚本错误终止并且表单正常提交。
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;
始终在浏览器的错误控制台打开的情况下开发JavaScript非常重要。您可能会看到有关未定义对象mypassword
的错误,这可能会将您指向代码中的错误行。
答案 1 :(得分:0)
如果没有html5标签,则使用公共className,它将是
<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 class="inputElement" type="text" name="username" id="username" ></td>
</tr>
<tr>
<td>Password*</td>
<td>:</td>
<td><input class="inputElement" type="text" name="password" id="password" pattern=".{5,}" title="Minimum length of 5 letters or numbers." ></td>
</tr>
<tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input class="inputElement" type="text" name="email" id="email" ></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>
<script type="text/javascript">
function ValidateLoginForm()
{
var inputElements = document.getElementsByClassName('inputElement');;
for(var i=0;i<inputElements.length;i++){
if(!inputElements[i].value){
alert(inputElements[i].name +' cannot be empty');
return false;
}
}
return true;
}
</script>