是的,这在很大程度上取决于你匹配正则表达式。
你应该首先尝试调试它:
(我将与Sarfraz的代码进行持续)。
$(this).keyup(function() {
if(!regex.test(this.value))
this.style.backgroundColor=config.invalidColor;
$('#debug').html('invalid color');
else
this.style.backgroundColor=config.validColor;
$('#debug').html('valid color!');
});
此外,由于您使用的是jQuery,因此您应该更多地依赖其'少写'的理念。
$(this).keyup(function() {
if(!regex.test(this.value))
$(this).css('backgroundColor', config.invalidColor);
$('#debug').html('invalid color');
else
$(this).css('backgroundColor', config.validColor);
});
也许你应该粘贴正则表达式代码和你想要实现的东西。
什么时候应该触发?这在很大程度上取决于你的正则表达式的样子。 – 2010-02-26 07:00:47