Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript表单验证if语句_Javascript_Html_Forms_Validation - Fatal编程技术网

javascript表单验证if语句

javascript表单验证if语句,javascript,html,forms,validation,Javascript,Html,Forms,Validation,我正在建立一个注册表,我发现Safari浏览器不能使用HTML5“必需”标签。因此,我构建了这个Javascript脚本,以确保用户必须提交数据,否则表单将不会提交 但是,如果您输入用户名和密码,但没有电子邮件地址,则表单仍会提交。(如果省略用户名和/或密码,则不提交表单) 所以问题似乎出在脚本的电子邮件地址部分。如果电子邮件字段为空,我不希望表单提交 这是我的密码: <script> function ValidateLoginForm() { var username =

我正在建立一个注册表,我发现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>

函数ValidateLoginForm()
{
var username=document.form1.username;
var password=document.form1.password;
var email=document.form1.email;
如果(username.value==“”)
{
警报(“您的用户名未被识别。”);
username.focus();
返回false;
}
如果(password.value==“”)
{
警报(“请输入密码”);
password.focus();
返回false;
}
如果(email.value==“”)
{
提醒(“请输入您的电子邮件地址”);
mypassword.focus();
返回false;
}
否则{
返回true;
}
}
这是我的表格(在桌子里)


登记表
用户名
:
密码*
:
电子邮件
:
*密码必须由数字和字母组成。。最少5个字符


谢谢

看起来您正在关注一个不存在的项目
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
,它可能会将您指向代码中的错误行。

如果没有html5标记,使用一个公共类名,它将是

<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>&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>


<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>

登记表
用户名
:
密码*
:
电子邮件
:
*密码必须由数字和字母组成。。最少5个字符

函数ValidateLoginForm() { var inputElements=document.getElementsByClassName('inputElement');;
for(var i=0;iThanks for the reply Michael。我刚刚尝试过,但即使电子邮件字段为空,它也会提交。我不知道为什么。@BobUni我完全不知道。现在我想我看到了上述问题的真正根源…谢谢Michael。这解决了它:-)我没有发现这一点,也没有意识到这会导致我的脚本终止并提交。每天学习一些新的东西!谢谢
<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>&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>


<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>