我有一个使用JQuery验证的联系表单,以便某些字段在发送之前必须包含一个值。我有两个字段(电话和电子邮件),其中一个或两个必须填写,所以我使用此代码验证:
<script>
$(document).ready(function() {
var nameok = 0;
var telephoneok = 0;
var emailok = 0;
var messageok = 0;
var validok = 1;
var contactok = 1;
// Name Validation
$('#name').blur(function() {
var name = $("#name").val();
if (name.length < 1) {
nameok = 0;
} else {
nameok = 1;
}
});
// Telephone Validation
$('#telephone').blur(function() {
var telephone = $("#telephone").val();
if (telephone.length < 1) {
telephoneok = 0;
} else {
telephoneok = 1;
}
});
// E-Mail Validation
$('#email').blur(function() {
var email = $("#email").val();
if (email.length < 1) {
emailok = 0;
} else {
emailok = 1;
}
});
// Message Validation
$('#message').blur(function() {
var message = $("#message").val();
if (message.length < 1) {
messageok = 0;
} else {
messageok = 1;
}
});
// Valid Validation
$('#valid').blur(function() {
var valid = $("#valid").val();
if (valid.length > 0) {
validok = 0;
} else {
validok = 1;
}
});
// Contact Validation
if (telephoneok == 0 && emailok == 0) {
contactok = 0;
} else {
contactok = 1;
}
// Submit button action
$('#submit').click(function() {
if (nameok == 1 && messageok == 1 && validok == 1 && contactok = 1) {
$('#submit').hide();
$('#form').submit();
} else {
$('#submit').show();
return false;
}
});
//End
});
</script>
但是,如果电话或电子邮件字段都不合适,它仍然会发送表格。我很确定错误在于Contact Validation
部分,但我错过了什么?