我无法让javascript函数验证是否返回true ...有人知道为什么吗?我的目标是添加JavaScript来验证表单,以便在输入框下显示错误。我已经实现了这一部分,我只是无法获得validate函数以返回true来提交表单。我的印象是我需要验证以返回正确的方式,因为我已经以HTML形式定义了功能
In the HTML the form is as such <form action ='reg.php' method ='POST' onsubmit='return function();'>
//markup
</form>
var form = document.getElementById('registration');
var errorDisplay = document.getElementById('nameError');
var errorEmail = document.getElementById('emailError');
var errorPassword = document.getElementById('passwordError');
var errorConfirmPassword = document.getElementById('passwordConfirmError');
var validateNameField = document.getElementById('FullNameField');
var validateEmailField = document.getElementById('emailField');
var validatePasswordField = document.getElementById('passwordField');
var validateConfirmPasswordField = document.getElementById('confirmPasswordField');
validateNameField.addEventListener('blur',checkName,true);
validateEmailField.addEventListener('blur',checkEmail,true);
validatePasswordField.addEventListener('blur',checkPassword,true);
validateConfirmPasswordField.addEventListener('blur',checkConfirmPassword,true);
function checkName(){
if(validateNameField.value !=''){
errorDisplay.innerHTML='';
return true;
}
}
function checkEmail(){
var emailRegEx =/\S+@\S+\.\S+/;
if(emailRegEx.test(validateEmailField.value)){
errorEmail.innerHTML='';
return true;
}
}
function checkPassword(){
if(validatePasswordField.value !=''){
errorPassword.innerHTML='';
return true;
}
}
function checkConfirmPassword(){
if(validateConfirmPasswordField.value !=''){
errorConfirmPassword.innerHTML='';
return true;
}
}
function validate(){
var formFields = document.getElementsByTagName('input');
var emailRegEx =/\S+@\S+\.\S+/;
if(formFields[0].name = 'FullName' && !(formFields[0].value)){
errorDisplay.innerHTML='name is required';
return false;
}
if (formFields[1].name = 'email' && !emailRegEx.test(formFields[1].value)){
errorEmail.innerHTML='valid email required';
return false;
}
if (formFields[2].name = 'password' && !(formFields[2].value)){
errorPassword.innerHTML='password is required';
return false;
}
if (formFields[3].name = 'password_confirmed' && !(formFields[3].value)){
errorConfirmPassword.innerHTML='confirm password is required';
return false;
}
if(formFields[2].value !== formFields[3].value){
errorConfirmPassword.innerHTML='passwrod does not mathch comfirm';
return false;
}
return true;
}