좋아, 그래서 나는 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
}); // end click function
잘 작동합니다. 그러나 조건부 성명서를 작성하여 이메일 또는 전화 중 하나에 내용이 있는지 확인하고 싶습니다. 나는 사람들이 둘 다 떠날 것을 강요하고 싶지 않지만 적어도 하나는 남겨 두어야한다.
전화가 콘텐츠 (전화 번호가 아닌 숫자 만)가 있으면 전자 메일은 더 이상 필수 항목이 아니며 그 반대의 경우도 마찬가지입니다. 전자 메일에 내용이있는 경우 유효한 전자 메일인지 확인해야합니다.
어떻게 이렇게 가겠어요? phone
가있는 경우 emai
리터가 확인되지 않았거나 다른 경우 email
이 ..phone 존재 있도록 내가 .. 여기 손실 :(
추가를 시도했지만 작동하지 않습니다. 보내기를 누르면 아직 두 가지를 묻습니다. – axelra82