Javascript表单验证(多个条件)

Javascript表单验证(多个条件),javascript,html,forms,Javascript,Html,Forms,我的表单目前检查两个电子邮件字段,以确保它们匹配。我还希望它确保这两个电子邮件字段都不是空的,并且复选框已被选中。我一直在寻找例子,但似乎没有任何效果 任何帮助都将不胜感激 <form action="" method="post" name="submission" onsubmit="return checkEmail(this);"> <label for="email">Email</label>

我的表单目前检查两个电子邮件字段,以确保它们匹配。我还希望它确保这两个电子邮件字段都不是空的,并且复选框已被选中。我一直在寻找例子,但似乎没有任何效果

任何帮助都将不胜感激

<form action="" method="post" name="submission" onsubmit="return checkEmail(this);">    
        <label for="email">Email</label>        
        <input id="email" maxlength="55" name="email" type="text" value="<?=htmlspecialchars($_POST["email"]) ?>" /><br/>   
        <label for="emailconfirm">Confirm email address</label>     
        <input id="emailconfirm" maxlength="55" name="emailconfirm" type="text" value="<?=htmlspecialchars($_POST["emailconfirm"]) ?>" /><br/>      
        <label for="checksign">By completing this form, you agree to the terms and conditions above. If you do not agree to these terms, your application will not be considered.</label>
        <input class="check" id="checksign" name="checksign[]" type="checkbox" value="Yes" /> 
</form>

<script type="text/javascript" language="JavaScript">
function checkEmail(theForm) {
    if (theForm.email.value != theForm.emailconfirm.value)
    {
        alert('E-mail addresses do not match.');
        return false;
    } else {
        return true;
    }    
}
</script> 

电子邮件

与其将其添加到
checkEmail()
函数中,我建议将其更改为一个完整的表单函数

函数validateForm(){
var email=document.getElementById('email');
var c_email=document.getElementById('emailconfirm');
var check=document.getElementById('checksign');
var错误=“”;
如果(email.value!=c_email.value){
error=“提供的电子邮件不匹配!”;
警报(错误);
返回false;
}else if(email.value.length==0 | | c|u email.value.length==0){
error=“提供的多封电子邮件中有一封是空的。”;
警报(错误);
返回false;
}否则如果(!check.checked){
error=“请接受我们的条款。”;
警报(错误);
返回false;
}
}


检查两个文本字段是否都有文本:

var regex = /\S+@\S+\.\S+/;
(typeof theForm.email.value !== 'undefined' && 
 typeof  theForm.emailconfirm.value !== undefined &&
 theForm.email.value.length > 1 && 
 theForm.emailconfirm.value.length > 1 && 
 regex.test(theForm.email.value)
 )

如果您使用AngularJS,您可以创建一个变量并将其设置为ng model=“[your variable here]”。
例如,代码可能如下所示:

                                     <input type="checkbox"
                                       ng-model="$scope.emailConfirm"/>

然后,您可以创建一个Javascript控制器,该控制器将评估模型的布尔值。使用条件(即if($scope.emailConfirm)//然后执行一些逻辑)来计算所述布尔值,并在该代码块中编写逻辑