How do I compare two values of an input type="password"?

1

I have a form in which a user would register. I need to know the password that he writes to compare it with the one he puts back to see that he was not wrong.

The code would be this:

	<script>
		function balidatu(){
			var pas1 = $("#pass").val();
            var pas2 = $("#passC").val();
            return();
        }
</script>
	
<!DOCTYPE html>
<html>
  <head>
    <meta name="tipo_contenido" content="text/html;" http-equiv="content-type" charset="utf-8">
	<title>Quizzes</title>
    <link rel='stylesheet' type='text/css' href='stylesPWS/style.css' />
	<link rel='stylesheet' 
		   type='text/css' 
		   media='only screen and (min-width: 530px) and (min-device-width: 481px)'
		   href='stylesPWS/wide.css' />
	<link rel='stylesheet' 
		   type='text/css' 
		   media='only screen and (max-width: 480px)'
		   href='stylesPWS/smartphone.css' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	

  </head>
  <body>
  <div id='page-wrap'>
	<header class='main' id='h1'>
      <span class="right"><a href='logIn.html'>LogIn</a> </span>
	<h2>Quiz: crazy questions</h2>
    </header>
	<nav class='main' id='n1' role='navigation'>
		<span><a href='layout.html'>Home</a></span>
		<span><a href='quizzes.html'>Quizzes</a></span>
		<span><a href='credits.html'>Credits</a></span>
	</nav>
    <section class="main" id="s1">
    
	<div>
			<form id="galderenF" name="galderenF" method="post" action="signUp.php" onsubmit="return balidatu()">
				
				<label for="posta"><strong>Posta (*):</strong></label>
				<input type="email" name="posta" id="posta" placeholder="Zein da zure posta?"  pattern="[a-zA-Z]{2,}[0-9]{3}@ikasle\.ehu\.(eus|es)"  required/><br>
			 
				<label for="deitura"><strong>Deitura (*):</strong></label>
				<input type="text" name="deitura" minlength="10" id="galdera" placeholder="Zein da zure galdera?" required/><br>
				
				<label for="nick"><strong>Nick (*):</strong></label>
				<input type="text" name="nick" id="erantzunZuzena" placeholder="Zein da zure erantzun zuzena?" required/><br>
				
				<label for="pasahitza" id="pass"><strong>Pasahitza (*):</strong></label>
				<input type="password" name="pasahitza" minlength = "6" id="erantzunOkerra" placeholder="Erantzun okerra 1" required/><br>
				
				<label for="pasEr" id="passC"><strong>Pasahitza Errepikatu (*):</strong></label>
				<input type="password" name="pasahitzaRep" minlength = "6" id="erantzunOkerra" placeholder="Erantzun okerra 2" required/><br>
				
				<input class="botoia" type="submit" id="botoia1" value="Bidali" />
				<input class="botoia" type="reset" id="botoia2" value="Reset" />
				<div id="aster" ><strong> * | Nahitaezko hutsuneak </strong></div><br><br>
			 
				
			</form>
    </section>
	<footer class='main' id='f1'>
		<p><a href="http://en.wikipedia.org/wiki/Quiz" target="_blank">What is a Quiz?</a></p>
		<a href='https://github.com'>Link GITHUB</a>
	</footer>
</div>
</body>
</html>

The problem is that when I pick it up like I'm taking it, it makes me feel as if the field is empty. It is as if the value were an empty String (something like this: "").

PS: style sheets are missing but it's not something important for me to doubt.

I edit to say that I forgot to comment that I tried this and it does not work:

var pas1 = $("#pass").val().replace(/./g,'*');

Greetings.

    
asked by Mikel Molinuevo 30.10.2017 в 19:33
source

2 answers

1

With comparing the values of the two fields you have. I have tried and val () returns the password entered in the password type field. So your validation function should be like this

<script>
    function balidatu(){
        var pas1 = $("#pass").val();
        var pas2 = $("#passC").val();
        return (pas1 === pas2);
    }
</script>

So that it returns true if they are equal and false if they are different, so you can use it in the onSubmit event of the form and send or not the data based on whether this aspect of your form is validated correctly.

    
answered by 30.10.2017 / 19:54
source
0

To compare you can use a condition in JavaScript. Ex:

function validarContraseña(){
    if($("#pass").val() === $("#passC").val()){
         //Si son iguales
         console.log("Las contraseñas son iguales");
    }else if($("#pass").val() !== $("#passC").val()){
         //Si no son iguales
         console.log("Las contraseñas no son iguales");
    }
}
    
answered by 31.10.2017 в 00:06