0

我知道这是不正确的,但我想知道如何实现一种验证,因此当输入正确时,背景变为绿色,或者如果不正确变为红色,让我们使用电子邮件示例。http://jsfiddle.net/dm6Hm/20/

<input type="email" class="Email" value="Email"/>
(".Email").change(function(e) {
    var $test = $(this).css("background-color", "");
    if ($test.val() == "email@email.com") {
        setTimeout(function() {
            $test.css("background-color", "green").focus();
        }, 0);
    }
});
4

3 回答 3

1
function isEmail(email){
    var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
    return reg.test(email);
}

$(".Email").keyup(function(e) {
    var $test = $(this).css("background-color", "");
    if (isEmail($test.val())) {
        setTimeout(function() {
            $test.css("background-color", "green").focus();
        }, 0);
    }else{
        setTimeout(function() {
            $test.css("background-color", "red").focus();
        }, 0);
    }
});
于 2013-07-14T05:04:53.553 回答
0
for correct input-->

   $('.Email').focus(function() {
        $('.Email').css('border','1px solid green');
    });

for incorrect input:

 $('.Email').blur(function() {
        $('.Email').css('border','1px solid red');
    });

你需要改变你的事件

于 2013-07-14T04:48:12.047 回答
0
$(".Email").keyup(function(e) {
    var $test = $(this).css("background-color", "");
    if ($test.val() === "email@email.com") {
        setTimeout(function() {
            $test.css("background-color", "green").focus();
        }, 0);
    }else{
        setTimeout(function() {
            $test.css("background-color", "red").focus();
        }, 0);
    }
});
于 2013-07-14T04:51:51.173 回答