我正在对我的表单进行大量验证,当它到达完成步骤时,我可以使用它this.form.submit();
来提交表单。到了那一步,我尝试使用console.log,它奏效了。但它只是不提交表单并收到此错误:
Uncaught TypeError: Cannot call method 'submit' of undefined
为什么它变得未定义?
$("#center").on('click', function(e) {
var valid = true;
var fields;
var canSubmit;
var readyForValidate;
var emailValidation;
var close = $("#close");
var errors = $(".errors");
var username = $("#username");
var pass = $("#password");
var repass = $("#repassword");
var pass_f = $("#password_f");
var repass_f = $("#repassword_f");
var email = $("#email");
var display = $("#display");
var incomplete = $(':input', this.form).filter(function() {
return $(this).val() == '';
});
$(':input', this.form).each(function(i, ele) {
e.preventDefault();
if (ele.value.trim() == "") {
ele.style.border = '2px solid red';
valid = false;
errors.html("One of the fields are empty, please fill them up. <span id='close'>X</span>");
errors.fadeIn("slow");
close.click(function() {
errors.fadeOut("slow");
errors.html("");
});
}
else
{
$(this).removeAttr('style');
}
});
if(!incomplete.length) {
fields = true;
} else {
fields = false;
}
if (fields) {
if (!isValidEmailAddress(email.val())) {
errors.html("The entered email address is not valid. <span id='close'>X</span>");
errors.fadeIn("slow");
email.css("border", "solid 2px red");
close.click(function() {
errors.fadeOut("slow");
errors.html("");
});
} else if (pass.val() != repass.val()) {
errors.html("Your passwords for in-game are not equal! <span id='close'>X</span>");
errors.fadeIn("slow");
pass.css("border", "solid 2px red");
repass.css("border", "solid 2px red");
} else if (pass_f.val() != repass_f.val()) {
errors.html("Your passwords for forums are not equal! <span id='close'>X</span>");
errors.fadeIn("slow");
pass_f.css("border", "solid 2px red");
repass_f.css("border", "solid 2px red");
} else if (pass.val().length < 5) {
errors.html("Your in-game password is too short! <span id='close'>X</span>");
errors.fadeIn("slow");
pass.css("border", "solid 2px red");
} else if (pass_f.val().length < 5) {
errors.html("Your forums password is too short! <span id='close'>X</span>");
errors.fadeIn("slow");
pass_f.css("border", "solid 2px red");
} else if (
pass_f.val() == repass_f.val() &&
pass.val() == repass.val() &&
isValidEmailAddress(email.val()) &&
pass.val().length > 4 &&
pass_f.val().length > 4) {
readyForValidate = true;
} else {
readyForValidate = false;
}
if (readyForValidate) {
validateUsername(username.val()).done(function(data) {
if ($.trim(data) == 1) {
errors.html("That username already exists! Please use another.<span id='close'>X</span>");
errors.fadeIn("slow");
username.css("border", "solid 2px red");
} else {
validateEmail(email.val(), 'email').done(function(data) {
if ($.trim(data) == 1) {
errors.html("That email is already registered. Please use another.<span id='close'>X</span>");
errors.fadeIn("slow");
email.css("border", "solid 2px red");
}
else
{
this.form.submit();
}
});
}
});
}
}
});
});
/**
* Function check email
* Checks if email is valid
*
* @param emailAddress Entered email address
* @return email is valid
**/
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
}
function validateUsername(username, type) {
return $.post("js/ajax/ajax.php", { validateUsername : 1 });
}
function validateEmail(username, type) {
return $.post("js/ajax/ajax.php", { validateEmail : 1 });
}