我想在WordPress中创建一个联系表单,其中必须填写两个字段之一才能发送。
例如 - e-mail
和telephone number
是必需的,但如果用户只填写一个字段,那么就可以了。
答案 0 :(得分:2)
这取决于您要如何验证它。
PHP?你可以这样做:
if ( empty($_POST['name1']) && empty($_POST['name2']) ) {
// Error message
}
JS?你可以这样做:
var input1Val = document.getElementsByName('name1')[0].value;
var input2Val = document.getElementsByName('name2')[0].value;
if ( input1Val.length > 0 || input2Val.length > 0 ) {
// Allow submission
}
答案 1 :(得分:0)
`$(document).ready(function () {
$('#forgot_pass_form').validate({ // initialize the plugin
groups: {
names: "uname email"
},
rules: {
uname: {
require_from_group: [1, ".send"]
},
email: {
require_from_group: [1, ".send"]
}
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
}
});
jQuery.extend(jQuery.validator.messages, {
require_from_group: jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields.")
});
});`
和html
<form action="#" class="send_form" id="forgot_pass_form" method="POST">
<fieldset>
<div class="send_row">
<label class="padding-top10">Email</label>
<input type="text" class="send_email send" id="email" name="email"
/> <em>You need to type an email address</em>
</div>
<div class="send_row option">OR</div>
<div class="send_row">
<label class="padding-top10">Username</label>
<input type="text" class="send_username send" id="uname"
name="uname" />
</div>
<div class="send_row send_submitforgotuser">
<input type="submit" value="Submit" />
</div>
</fieldset>
</form>
<a id="docs" href="http://docs.jquery.com/Plugins/Validation" target="_blank">Validation Documentation</a>
答案 2 :(得分:0)