[英]How to enable validation of select option hide/show div-input only when it selected?
I have a select option box that when I select it, a div-input appear. 我有一个选择选项框,当我选择它时,将显示一个div输入。 The problem that when I put validation on the this div-input, and when i submit even when it hide, it require me to validate it, thus giving me error msg.
问题是,当我在此div输入上进行验证时,即使隐藏时我也提交了该问题,它要求我对其进行验证,从而给我错误消息msg。
How to do enable validation for this input field only when if it appear? 仅当此输入字段出现时,如何启用验证?
PHP 的PHP
<div class="form-group">
<label for="inquiry_type">Subject *: </label>
<select name="type" id="inquiry_type" class="form-control">
<option value="0">Please Select</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="others">Other...</option> //div-input appear when sel
</select>
</div>
<div id="other_subj" class="form-group">
<label for="other_msg_subj">Input Subject Title *: </label>
<input type="text" id="other_msg_subj" class="form-control" name="other_msg_subj" placeholder="Enter Subject Title" required/>
</div>
JQUERY for hide/show 用于隐藏/显示的JQUERY
<script>
$(function() {
$('#other_subj').hide();
$('#inquiry_type').change(function(){
if($('#inquiry_type').val() === 'others') {
$('#other_subj').show();
} else {
$('#other_subj').hide();
}
});
});
var inquiry_other_subj = document.getElementById("other_msg_subj").value;
...
} else if (!inquiry_other_subj.match(/^(?=.*[A-Za-z ]).{5,20}$/)) {
alert("Need to enter a proper inquiry title containing only letter!"); // I want this to be active only when the div is shown
} else {
var parameters="...+"&other_msg_subj="+inquiry_other_subj;
PHP validation PHP验证
//Same for this php validation
...
else if(!preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['other_msg_subj'])){
echo "Please enter a valid message subject title!";
exit();
}
First, you need to disable the input when it's hidden to prevent sending it's data to the server: 首先,您需要在隐藏输入时将其禁用,以防止将其数据发送到服务器:
$(function() {
$('#other_subj').hide();
$('#inquiry_type').change(function(){
if($('#inquiry_type').val() === 'others') {
$('#other_msg_subj').prop('disabled', false);
$('#other_subj').show();
} else {
$('#other_msg_subj').prop('disabled', 'disabled');
$('#other_subj').hide();
}
});
});
And you need to do a check before validating: 并且您需要在验证之前进行检查:
On Javascript side: 在Javascript方面:
else if (
$("#other_msg_subj").is(":visible") &&
!inquiry_other_subj.match(/^(?=.*[A-Za-z ]).{5,20}$/)) {
On PHP side: 在PHP方面:
else if(isset($_POST['other_msg_subj']) &&
!preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['other_msg_subj'])){
This may help. 这可能会有所帮助。
in JQUERY : 在JQUERY中 :
Validate only if others
is selected. 仅在选择
others
选项时验证。
var inquiry_type = $('#inquiry_type').val();
else if (inquiry_type === 'others' && !inquiry_other_subj.match(/^(?=.*[A-Za-z ]).{5,20}$/)) {
alert("Need to enter a proper inquiry title containing only letter!");
}
Add the other_msg_subj
parameter in the variable parameters
only if others
is selected. 仅在选择
others
参数的情况下,在变量parameters
添加other_msg_subj
参数。
var parameters = "......"; //Other parameters
if(inquiry_type == "others")
{
parameters = parameters + "&other_msg_subj="+inquiry_other_subj;
}
in PHP : 在PHP中 :
Check if only the parameter is present 检查是否仅存在参数
else if(!isset($_POST['other_msg_subj']) && !preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['other_msg_subj']))
{
echo "Please enter a valid message subject title!";
exit();
}
Also (Just a suggestion) you can simplify the JQUERY code : 另外(只是一个建议),您可以简化JQUERY代码:
show/hide
as toggle(true/false)
true = show(), false = hide()
show/hide
为toggle(true/false)
true = show(), false = hide()
$(function() {
$('#other_subj').hide();
$('#inquiry_type').change(function(){
var display = $('#inquiry_type').val() === 'others';
$('#other_subj').toggle(display);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.