我想检查是否有错误,但现在它没有检查是否有任何错误。即使字段为空,表单也会提交。不知道故障在哪里。有人可以帮助表格中的所有字段都是正确的我认为if(error == 0)
:
<script>
$(document).ready(function(){
var errormsg = ['Please enter name.', 'Please enter minimum 3 character.', 'Value not more than 100 characters.', 'Please enter email.', 'Please enter valid email.', 'Email and confirm email does not match.', 'Please enter password', 'Please enter role.', 'Email already exists'];
$("input[type='submit']").on('click', function(e){
e.preventDefault();
$(".validationError").remove();
var span_error_start = '<span class="form_error">';
var span_error_end = "</span>";
var name = $.trim($("input[name='name']").val());
var problem = $.trim($("input[name='problem']").val());
var description = $.trim($("input[name='description']").val());
var error = 0;
if(name == ""){
$("input[name='name']").after("<div class='nameInput validationError'>"+span_error_start+errormsg[0]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
else if(name.length < 3){
$("input[name='name']").after("<div class='nameInput validationError'>"+span_error_start+errormsg[1]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
else if(name.length > 100){
$("input[name='name']").after("<div class='nameInput validationError'>"+span_error_start+errormsg[2]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
if(problem == ""){
$("input[name='problem']").after("<div class='problemInput validationError'>"+span_error_start+errormsg[3]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
else if(problem.length < 3){
$("input[name='problem']").after("<div class='problemInput validationError'>"+span_error_start+errormsg[1]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
else if(problem.length > 100){
$("input[name='problem']").after("<div class='problemInput validationError'>"+span_error_start+errormsg[2]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
if(description == ""){
$("input[name='description']").after("<div class='descriptionInput validationError'>"+span_error_start+errormsg[6]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
else if(description.length < 3){
$("input[name='description']").after("<div class='descriptionInput validationError'>"+span_error_start+errormsg[1]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
else if(description.length > 100){
$("input[name='description']").after("<div class='descriptionInput validationError'>"+span_error_start+errormsg[2]+span_error_end+"</div>").addClass("input_error");
error = 1;
}
if(error == 0){
$(".myForm").submit();
}
}
else{
return false;
}
});
</script>
答案 0 :(得分:1)
代码出错。额外的括号,所以否则不起作用。语法错误:
if(error == 0){
$(".myForm").submit();
}
} // Extra braces, so else doesn't work. Syntax error. So remove it.
else {
return false;
}
更新了正确的代码:
if(error == 0){
$(".myForm").submit();
}
else {
return false;
}
答案 1 :(得分:0)
解决了问题:
if(error == 0){
$(".myForm").submit();
}
else{
return false;
}
});