Saturday, June 7, 2014

JavaScript Check Password / Confirm Password

This is very important for registration.I have used onKeyUp() to check password/confirm are same. and get values using document.getElementById('') function.


JQuery Function

      <script>
        function checkPasswords()
        {  
            var password1 = document.getElementById('password1');
            var password2 = document.getElementById('password2');
            var message = document.getElementById('alertMessage');
            var goodColor = "#66cc66";
            var badColor = "#ff6666";
            if(password1.value == password2.value){
                password2.style.backgroundColor = goodColor;
                message.style.color = goodColor;
                message.innerHTML = "Passwords Match!"
            }else{
                password2.style.backgroundColor = badColor;
                message.style.color = badColor;
                message.innerHTML = "Passwords Do Not Match!"
            }
        }
</script>

HTML

 <form>
      <div class="fieldWrapper"> Password:
        <input type="password" name="pass1" id="password1">
        Confirm Password:
        <input type="password" name="pass2" id="password2" onKeyUp="checkPass(); return false;">
        <span id="alertMessage" class="alertMessage"></span> </div>
    </form>

Please share your comments and feedback.Thanks.Please subscribe my updates via email.

No comments:

Post a Comment