hapro Posted August 14, 2015 at 09:44 PM Report #587058 Posted August 14, 2015 at 09:44 PM boas, eu tenho aqui uma problema. è assim eu fiz este codigo, mas está a dar-me erro e não sei a onde. O que quero fazer é comparar as palavras-passes se der continua se não fica nesta pagina a dizer onde está o erro. obrigado. Aqui está o codigo <html> <head> <title>Validar a palavra-passe</title> <script> var password = document.getElementById("password") , confirm_password = document.getElementById("confirm_password"); function validatePassword(){ if(password.value != confirm_password.value) { confirm_password.setCustomValidity("Senhas diferentes!"); } else { confirm_password.setCustomValidity(''); } } password.onchange = validatePassword; confirm_password.onkeyup = validatePassword; </script> </head> <body> VALIDAR SENHA <br> <br> <form class="pure-form"action="registar.php"> <fieldset> <legend>Confirmação da palavra-passe </legend> <input type="password" placeholder="palavra-passe" id="password" required> <input type="password" placeholder="Confirme palavra-passe" id="confirm_password" required> <button type="submit" class="pure-button pure-button-primary">Confirmar</button> </fieldset> </form> </body> </html> Melhores cumprimentos, HAPRO
Bernardo Vieira Posted August 15, 2015 at 11:46 AM Report #587078 Posted August 15, 2015 at 11:46 AM A unica coisa que estás a fazer mal, é que estás a usar "getElementById" ainda antes de criar o elemento, logo ele nunca o encontra, logo nao acontece nada. Tens duas opções, 1. faz uma pequisa por "jquery document ready" e tentar perceber o que deves mudar (o código dentro dessa função só será chamado quando toda a página html estiver carregada) 2. pões o script depois, tipo isso <html> <head> <title>Validar a palavra-passe</title> </head> <body> VALIDAR SENHA <br> <br> <form class="pure-form"action="registar.php"> <fieldset> <legend>Confirmação da palavra-passe </legend> <input type="password" placeholder="palavra-passe" id="password" required> <input type="password" placeholder="Confirme palavra-passe" id="confirm_password" required> <button type="submit" class="pure-button pure-button-primary">Confirmar</button> </fieldset> </form> <script> var password = document.getElementById("password") , confirm_password = document.getElementById("confirm_password"); function validatePassword(){ if(password.value != confirm_password.value) { confirm_password.setCustomValidity("Senhas diferentes!"); } else { confirm_password.setCustomValidity(''); } } password.onchange = validatePassword; confirm_password.onkeyup = validatePassword; </script> </body> </html>
HappyHippyHippo Posted August 15, 2015 at 12:29 PM Report #587079 Posted August 15, 2015 at 12:29 PM https://www.portugal-a-programar.pt/topic/71653-confirmar-a-palavra-passe/ IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now