How to adapt this login system in JavaScript?

1

I need to create a simple internal login system to apply to a blog. I can not make my code work at all, and I need help.

This is the Code:

<script language="JavaScript">
	function Login(){
		var done=0;
		var usuario=document.login-inputs.usuario.value;
		usuario=usuario.toLowerCase();
		var senha=document.login-inputs.senha.value;
		seha=senha.toLowerCase();
		if (usuario=="admin" && senha=="admin") {
			window.location="/p/admin.html"; done=1;
		}
		if (done==0) { alert("Dados incorretos, tente novamente"); }
	}
	</script>
<div id="all">
	<div id="login-box">
		<div id="login-header">
			Faça login no sistema
		</div>
		<div id="login-inputs">
			<input type="text" placeholder="Nome de usuário" name=usuario>
			<br />
			<input type="password" placeholder="Senha" name=senha>
		</div>
		<div id="enviar">
			<input type="button" onclick="Login()" class="botao" value=Login>
			<a href="#">Esqueceu a sua senha?</a>
		</div>
	</div>
</div>

Does anyone set the script please? (I can not modify the HTML)

    
asked by anonymous 07.04.2015 / 13:30

2 answers

5

First a alert : avoid authenticating through JavaScript as it is extremely insecure, an example, someone just view the source code of the page to see the user and password, as well as other problems. As warned bfavaretto is almost like having no authentication.

Well, you say you can not change the HTML, but there are some errors in it, such as the lack of quotation marks in the% value attribute of the inputs attribute. I've also changed how you capture the inputs values in script . I recommend that you define an ID in inputs , because when you get the name, if there is more with the same name, there may be problems.

function Login() {
  var done=0;
  var usuario = document.getElementsByName('usuario')[0].value;
  usuario=usuario.toLowerCase();
  var senha= document.getElementsByName('senha')[0].value;
  seha=senha.toLowerCase();
  if (usuario=="admin" && senha=="admin") {
    window.location="/p/admin.html";
    done=1;
  }
  if (done==0) { alert("Dados incorretos, tente novamente"); }
}
<div id="all">
  <div id="login-box">
    <div id="login-header">
      Faça login no sistema
    </div>
    <div id="login-inputs">
      <input type="text" placeholder="Nome de usuário" name="usuario">
      <br />
      <input type="password" placeholder="Senha" name="senha">
    </div>
    <div id="enviar">
      <input type="button" onclick="Login()" class="botao" value="Login">
      <a href="#">Esqueceu a sua senha?</a>
    </div>
  </div>
</div>
    
07.04.2015 / 13:47
-2
function Login() {
  var done=0;
  var usuario = document.getElementsByName('usuario')[0].value;
  usuario=usuario.toLowerCase();
  var senha= document.getElementsByName('senha')[0].value;
  seha=senha.toLowerCase();
  if (usuario=="admin" && senha=="admin") {
    window.location="/p/admin.html";
    done=1;
  }
  if (done==0) { alert("Dados incorretos, tente novamente"); }
}
<div id="all">
  <div id="login-box">
    <div id="login-header">
      Faça login no sistema
    </div>
    <div id="login-inputs">
      <input type="text" placeholder="Nome de usuário" name="usuario">
      <br />
      <input type="password" placeholder="Senha" name="senha">
    </div>
    <div id="enviar">
      <input type="button" onclick="Login()" class="botao" value="Login">
      <a href="#">Esqueceu a sua senha?</a>
    </div>
  </div>
</div>
    
19.11.2018 / 11:53