我是javascript的新手,并且很难尝试编写条件语句,如果输入字段为空/空白,或者除了a-z之外还包含任何其他字符,则会给出错误。
这是我到目前为止所做的:
$(document).ready(function(){
$('input#first, #city, #subject, input#last, textarea#message').on("keyup bind cut copy paste", function(){
var value = $(this).val();
var first = new RegExp('[a-z]');
if (/^\s*$/.test(first.value));
if(value=='' || first.length<=1) {
if(value=='' || last.length<=1);
if(value=='' || city.length<=1);
if (first.test(value));
$(this).css('border','2px solid #ff0000');
$(this).css('background-color','#ffcece');
}
else {
$(this).css('border','1px solid #ffd09d');
$(this).css('background-color','#ffffff');
}
});
});
答案 0 :(得分:1)
这应该对你有用
$(document).ready(function(){
$('input#first, #city, #subject, input#last, textarea#message').on("keyup bind cut copy paste",
function(){
var value = $(this).val();
var first = new RegExp('^[a-zA-Z]+$');
if (first.test(value)) {
$(this).css('border','2px solid #ff0000');
$(this).css('background-color','#ffcece');
}
else {
$(this).css('border','1px solid #ffd09d');
$(this).css('background-color','#ffffff');
}
});
});
答案 1 :(得分:0)
尝试类似这样的事情:
document.getElementById("yourInputPanel").addEventListener("onchange",function(){
if(document.getElementById("yourInputPanel").search(" ") > -1){
...
Handling Code
...
}
});