2014-02-28 105 views
0

我有一个简单的注册表格,我决定添加确认密码的功能,即使我输入不同的密码也不会检查!
我的代码中是否有错误?密码确认Javascript

$(document).ready(function(){ 
$('input[name=c_password]').keyup(password_check); 

}); 
function password_check(){ 
var password = $('input[name=password]').val(); 
var c_password = $('input[name=c_password]').val(); 
if(c_password != password || c_password.length < 6){ 
    //alert("test"); 
    $('input[name=c_password]').parent().addClass("has-error has-feedback"); 
}else{ 
    $('input[name=c_password]').parent().removeClass("has-success has-feedback"); 

} 
} 

谢谢!

演示:http://jsfiddle.net/52VtD/3217/

+2

你在期待,如果密码不匹配的情况发生? – Phil

+0

我想输入框的边框变为红色 – user3350731

+0

您是否因为原因删除了网址? :) –

回答

3

您的代码几乎是正确的,只是你忘记删除在正确的c_password的情况下,类有错误。

$('input[name=c_password]').parent().removeClass("has-error") 

http://jsfiddle.net/52VtD/3221/

+1

谢谢!我完全忘了!我现在更新它:http://jsfiddle.net/52VtD/3222/ – user3350731

+1

您不需要删除if-then-else内部的类,因为它们在函数的开头被删除 – marciojc

+0

您实际上对 ! – user3350731

1
if(c_password != password || c_password.length < 6){ 
    //alert("test"); 
    $('input[name$=password]').parent().addClass("has-error has-feedback"); 
}else{ 
... 
    (you need put a 'url' on ajax) 
1

它看起来像你刚刚中省略一些代码。从您提供什么我猜你想是这样的:

if(c_password != password || c_password.length < 6){ 
    // This is the additional code 
    $('input[name=c_password]').parent().addClass("has-error has-feedback"); 
}else{ 

    jQuery.ajax({ 
     success: function(response){ 
... 
} 
} 

东西沿着这些线路应该突出一个错误给用户。不要忘记验证服务器端,客户端只是为了方便用户。

编辑:实际上,我们忽略了最后的编辑:/

+0

不错。你的代码就像我的,但我已经downvote? – Falci

+0

不是从我,不知道为什么发生。 – Phil

+0

我很抱歉,但我没有工作! http://jsfiddle.net/52VtD/3217/ – user3350731