我有一个简单的表单,用户指定一个名称,问题是:
website_name ===“”|| !/ ^ [A-Z] * $ /
永远不是真的,当我输入一个数字时,它永远不会让背景变红,为什么?
$( "#website_name" ).keyup(function() {
var message;
if(website_name==="" || !/^[a-z]*$/)
{
$("#website_name").css('border', '3px solid red', 'background', '#f27676');
message = 'Only lower case characters allowed!';
$('div.modal-body').append(message);
}else{
$("#website_name").css('border', '3px solid green', 'background', '#56c145');
message = 'Your name is correct';
$('div.modal-body').append(message);
}
});
答案 0 :(得分:1)
您需要.test()
值:
!/^[a-z]*$/.test(this.value)
console.log(!/^[a-z]*$/.test('123'));

答案 1 :(得分:0)
您需要针对您的website_name变量测试正则表达式。
$("#website_name").keyup(function () {
var message;
var pattern = new RegExp("^[a-z]*$");
if (website_name === "" || pattern.test(website_name) === false) {
$("#website_name").css('border', '3px solid red', 'background', '#f27676');
message = 'Only lower case characters allowed!';
$('div.modal-body').append(message);
} else {
$("#website_name").css('border', '3px solid green', 'background', '#56c145');
message = 'Your name is correct';
$('div.modal-body').append(message);
}
});