2010-02-26 67 views
0

我有一些JavaScript代码,上面写着:与现场验证脚本问题

$(this).keyup(function() { 
    if(regex.test(this.value)) 
    this.style.backgroundColor=config.validColor; 
else 
    this.style.backgroundColor=config.invalidColor; 
}); 

这应该提供的视觉提示用户,约场,他或她打字的,然而,这只是翻转每个按键的颜色。

+0

什么时候应该触发?这在很大程度上取决于你的正则表达式的样子。 – 2010-02-26 07:00:47

回答

0

不知道该怎么回应test函数返回时,你可以尝试这样的:

$(this).keyup(function() { 
    if(!regex.test(this.value)) 
    this.style.backgroundColor=config.invalidColor; 
else 
    this.style.backgroundColor=config.validColor;   
}); 
0

是的,这在很大程度上取决于你匹配正则表达式。

你应该首先尝试调试它:

(我将与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); 
}); 

也许你应该粘贴正则表达式代码和你想要实现的东西。