我有这样的功能:
function validateInput() {
var search_text = document.getElementById('search_text').value;
var size = document.getElementById('size').value;
var submitButton = document.getElementById('sb_search');
var filter = /^[\x20-\x7E]*$/;
if (filter.test(search_text) && search_text.length>0){
return true;
}
else if (filter.test(size) && search_text.length>0){
return true;
}
else{
return false;
}
}
我想只在search_text不为空时返回true,满足正则表达式并且不超过15个字符。我怎样才能做到这一点?第1和第2已经完成。
答案 0 :(得分:1)
function validateInput() {
var search_text = document.getElementById('search_text').value;
var size = document.getElementById('size').value;
var submitButton = document.getElementById('sb_search');
var filter = /^[\x20-\x7E]*$/;
if (filter.test(search_text) && search_text.length>0){
return true;
}
else if (filter.test(size) && search_text.length>0){
return true;
}
else if(search_text.length>=15){
return false;
}
}
答案 1 :(得分:0)
希望你喜欢这个
if (filter.test(search_text) && search_text.length>0 && search_text.length<= 15)
{
return true;
}
else
return false