/ Published in: JavaScript
I just put all the functions in here
the validateConfPass function uses the key event and the two text fields to change the background color of the confPassword field when it does not equal the pass field
the validateConfPass function uses the key event and the two text fields to change the background color of the confPassword field when it does not equal the pass field
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<html> <head> <title>password confirm</title> <script type="text/javascript"> function validateConfPass(e, confPassword, pass) { keychar = getCharacterPressed(e); if (keychar != "-1") { if (keychar == "backspace") { lenOfConf = confPassword.value.length - 1; if (confPassword.value.substr(0, lenOfConf) != pass.value.substr(0, lenOfConf)) confPassword.style.background = "#FFBBBB"; else confPassword.style.background = "#FFFFFF"; } else { lenOfConf = confPassword.value.length + 1; if (confPassword.value + keychar != pass.value.substr(0, lenOfConf)) confPassword.style.background = "#FFBBBB"; else confPassword.style.background = "#FFFFFF"; } } } function getCharacterPressed(e) { code = getKeyCode(e); keychar = "-1"; if (pressedPrintableChar(code)) keychar = String.fromCharCode(code); if (code == 8) keychar = "backspace"; return keychar; } function getKeyCode(e) { code = -1; if (window.event) code = e.keyCode; else if (e.which) code = e.which; return code; } function pressedPrintableChar(code) { if (code != 8 && code != 13 && code + "" != "undefined") return true; return false; } </script> </head> <body onload="javascript: document.frm.password.focus();"> this code highlights the confirm password field when it is different from the password field<br /> <form name="frm"> password: <input type="password" name="password" id="password*" /><br /> confirm password: <input type="password" id="confPass" onkeypress="javascript: validateConfPass(event, this, document.getElementById('password*'));" /><br /> <input type="submit" value="Submit" /> </form> </body> </html>
URL: http://justpushbuttons.com/examples/password_example.php