2015-02-05 2 views
0

내 양식은 현재 두 개의 이메일 필드가 일치하는지 확인합니다. 두 전자 메일 필드가 비어 있고 확인란이 선택되어 있지 않은지 확인하는 것이 좋습니다. 나는 예제를 위해 수색을하고 있었고 아무것도 트릭을하지 않는 것처럼 보인다.자바 스크립트 양식 유효성 확인 (복수 조건)

도움을 주시면 감사하겠습니다. 오히려 I는 전체 형상 함수로 변경 제언 checkEmail() 기능에 부가보다

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

답변

1

. 그들은 단지 이메일 입력에 등 하나 개 또는 두 개의 공간에 넣어하지 않았다

이 유효한 이메일 확인하지 않습니다 그것을 작동 않지만

function validateForm() { 
 
    
 
    var email = document.getElementById('email'); 
 
    var c_email = document.getElementById('emailconfirm'); 
 
    var check = document.getElementById('checksign'); 
 
    var error = ''; 
 
    
 
    if (email.value != c_email.value) { 
 
     error = "The provided emails do not match!"; 
 
     alert(error); 
 
     return false; 
 
    } else if (email.value.length == 0 || c_email.value.length == 0) { 
 
     error = "One of more of the emails provided are empty."; 
 
     alert(error); 
 
     return false; 
 
    } else if (!check.checked) { 
 
     error = "Please accept our terms."; 
 
     alert(error); 
 
     return false; 
 
    } 
 
    
 
}
<form action="" method="post" name="submission" onsubmit="return validateForm();"> 
 
    <!-- controls.. --> 
 
</form
Example

. 그건 일부 정규식에 대한 그들의 입력을 확인해야합니다.

1

검사 텍스트 필드 모두 텍스트를 가지고 :

var regex = /\[email protected]\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) 
) 
0

당신이 변수를 만들고 동일 = "[당신의 변수 여기]"모델을 겨 설정할 수 AngularJS와를 사용한 경우.
예를 들어, 코드는 다음과 같습니다

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

당신은 부울의 모델을 평가하는 자바 스크립트 컨트롤러를 만들 수 있습니다. 조건부 (즉 if ($ scope.emailConfirm) // 그런 다음 일부 논리를 사용하여) 부울을 평가하고 그 코드 블록에서 논리를 작성하십시오.