我有一个这样的Javascript函数:
function validateInput() {
var search_text = document.getElementById('search_text').value;
var size = document.getElementById('size').value;
var submitButton = document.getElementById('sb_search');
submitButton.removeAttribute('disabled');
var filter = /^[\x20-\x7E]+$/;
if (filter.test(search_text) && filter.test(size)) {
return true;
} else {
submitButton.setAttribute('disabled', 'disabled');
}
}
如果输入字段 search_text 和 size 满足正则表达式,它应该允许提交表单。但即使我输入了正确的值,它也不允许我提交表单。如果我只使用这样的条件,它就可以工作:
if (filter.test(search_text)){
return true;
}
我希望两个输入字段都满足正则表达式。我错过了什么?
编辑 HTML
<form action="/search/" id="ui_element" class="sb_wrapper">{% csrf_token %}
<!-- <p> <span class="sb_down"></span> -->
<input class="sb_input" type="text" id="search_text" name="search_text" placeholder="Search by Name ...">
<input class="sb_search" id="sb_search" type="submit" value="Go" onclick="validateInput()">
</p>
<ul class="sb_dropdown" style="display:none;">
<li class="sb_filter">Filter your search</li>
<li>
<select id="selectmode" name="selectmode" class="filter">
<option value="filter">Filter</option>
<option value="date">By date</option>
<option value="size">By size</option>
</select>
</li>
<li class="bydateinputs" style="display: none">
<label>Date:</label>
<input type="text" id="date_picker" style="width:100px;" name="date" class="text_search" />
</li>
<li class="bysizeinputs" style="display: none">
<label>Size:</label>
<input type="text" id="size" style="width:100px;" name="size" placeholder="eg: 4 MB" class="text_search" />
</li>
</ul>
</form>