Jump to content

Recommended Posts

Posted

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

Posted

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>

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.