使用JQuery进行表单验证(如果/其他) [英] Form validation if/else with JQuery
问题描述
我正在进行表单验证,所有字段都必须填写,并检查用户是否接受条款",并检查电子邮件是否正确.几乎可以正常工作,如果我没有输入姓名/电子邮件等,此代码会给我答复,但如果输入此信息,则不会隐藏(.alert).alert.然后不响应条款和有效电子邮件"复选框的复选框.如果没有响应",也应该始终隐藏"警报.
I'm making a form validation, where all fields should be required and also check if the user "accept terms", and also check if the e-mail is correct. It almost works, this code gives me a response if I haven't entered name/e-mail etc but if I enter this information, it doesn't hide() the .alert. And then doesn't response to the checkbox for terms and "valid e-mail" check. The .alert should also always be "hidden" if there are no "response".
此代码在哪里失败?有什么想法吗?
Where does this code fail? Any thoughts?
$("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)
}
推荐答案
这段代码可以正常运行,但是我不确定您的逻辑是否正确.
Hi this code will work without errors , but i am not sure about your logic is correct.
$("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);
}
这篇关于使用JQuery进行表单验证(如果/其他)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!