好吧,所以我一直在四处寻找条件语句在JQuery中,但我无法找到一个解决方案如何检查,以确保至少有一个两个输入框有内容。联系表单,填写两个输入字段之一检查
这是我迄今为止
$('#send').click(function(){ // when the button is clicked the code executes
$('.error').fadeOut('fast'); // reset the error messages (hides them)
var error = false; // we will set this true if the form isn't valid
var name = $('input#namn').val(); // get the value of the input field
var message = $('textarea#meddelande').val(); // get the value of the textarea
var phone = $('input#telefon').val(); // get the value of the input field
var email_compare = /^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/; // Syntax to compare against input
var email = $('input#epost').val(); // get the value of the input field
if(name == "" || name == " " || name == "namn" || name.length < 2) {
$('input#namn').val("namn").css({ 'color': 'red' });
error = true; // change the error state to true
}
if(phone == "" || phone == " " || phone == "telefon" || phone.length < 5) {
$('input#telefon').val("telefon").css({ 'color': 'red' });
error = true; // change the error state to true
}
if (email == "" || email == " " || email == "epost") { // check if the field is empty
$('input#epost').val("epost").css({ 'color': 'red' });
error = true;
}else if (!email_compare.test(email)) { // if it's not empty check the format against our email_compare variable
$('input#epost').val("kontrollera epost").css({ 'color': 'red' });
error = true;
}
if(message == "" || message == " " || message == "meddelande" || message.length < 10) {
$('textarea#meddelande').val("meddelande").css({ 'color': 'red' });
error = true; // change the error state to true
}
if(error == true) {
$('#err-form').fadeIn('fast');
return false;
}
var data_string = $('#ajax-contactform').serialize(); // Collect data from form
$.ajax({
type: "POST",
url: $('#ajax-contactform').attr('action'),
data: data_string,
timeout: 6000,
error: function(request,error) {
if (error == "timeout") {
$('#err-timedout').fadeIn('fast');
}
else {
$('#err-state').fadeIn('fast');
$("#err-state").html('Ett fel uppstod: ' + error + '');
}
},
success: function() {
$('#ajax-contactform').fadeOut('fast');
$('#success-msg').fadeIn('slow');
}
});
return false; // stops user browser being directed to the php file
}); //结束点击功能
而且其工作的罚款。但是,我想提出一个条件声明来检查并确保电子邮件或电话有内容。我不想迫使人们不得不离开这两个,但至少有一个。
因此,如果手机有内容(只有数字,没有单词“电话”),则电子邮件不再是强制性的,反之亦然。如果电子邮件有内容,则仍需检查以确保其有效的电子邮件。
我该怎么做呢?我在一个有点丢在这里:(
我试着添加这个,但它不起作用。当发送它仍然要求两个。 – axelra82 2013-03-28 10:19:09