我正在進行表單驗證,所有字段都應該被要求,並且檢查用戶是否「接受條款」,並檢查電子郵件是否正確。它幾乎可以工作,如果我沒有輸入姓名/電子郵件等,這段代碼給了我一個迴應,但如果我輸入這個信息,它不會隱藏().alert。然後不回覆條款和「有效的電子郵件」檢查複選框。如果沒有「響應」,.alert也應該始終「隱藏」。表單驗證if/else與JQuery
此代碼在哪裏失敗?有什麼想法嗎?
$("form").submit(function (e) {
e.preventDefault(); // This will prevent the form submission
var response = "";
$('#submit-holder').find('input').each(function(){
if ($(this).val() == '') {
response += ", " + $('label[for="' + this.id + '"]').html();
empty_fields = true;
}
else if ($(this).val() == !'') {
empty_fields = false;
$('.alert').hide()
}
else if(empty_fields = true) {
$('.alert').addClass('alert-danger')
$('.alert').fadeIn()
$('.error_message').text(response.replace(", ", "You need to fill out: "))
message = response.replace(", ", "You need to fill out: ")
response_message(message)
}
else if ($('[name="txtEmail"]').length !== 0) {
var email = $('[name="txtEmail"]').val();
if(IsEmail(email)==false){
message = "The e-mail address you've entered is invalid";
response_message(message)
//return false;
}
}
else if($('#user_terms').not(":checked")){
message = "You need to accept the terms and conditions to continue";
response_message(message)
//return false;
}
});
if($('#user_terms').is(":checked") && !response) {
// Send the form
}
// Functions:
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!regex.test(email)) {
return false;
}else{
return true;
}
}
function response_message(message) {
$('.alert').addClass('alert-danger')
$('.alert').fadeIn()
$('.error_message').text(message)
}
否則,如果($(本).VAL()==「」!){ - 看起來像一個罪犯...... –
看起來好像你缺少了一些行結束分號; – RedEyedMonster
[JQuery表單不會驗證成功]的可能重複(http://stackoverflow.com/questions/18379864/jquery-form-wont-validate-to-success) – mplungjan