Uncaught Typeerror: Cannot read Property 'value' of Undefined

Asked

Viewed 461 times

1

I have a form that will only give Submit if the password has only letters and numbers, and after this check, should redirect to a link. Form code:

<form onsubmit="return Redirect(err, resp, body, password);">
  <div class="form-group">
    <label class="password-text" for="password">Senha</label><br>
    <input id="password" type="password" placeholder="Insira a senha" name="password" title = "Precisa contar no máximo 6 caracteres entre letras e números." required >
  </div>
  <button type="submit" class="form-button"">Acessar</button>
</form>

JS Code:

var request = require('request');
var psw = /^[a-zA-Z0-9-]\w{1,6}$/;
request.get('Link X', function Redirect(err, resp, body, password){
  if(password.value.match(psw)){
    alert('Senha válida, redirecionando ..');
    document.location.assign(body);
    return false;
  }
  else{
    alert('Senha inválida. Tente novamente.');
    return true
  }
});

To make it clearer, 'Link X', has only one link in your <body>, which is the link I want to redirect, so I check the password typed to see if it matches the filters, and after that I will redirect to the 'body' that has the link. However I am getting error on my 'if': 'Uncaught Typeerror: Cannot read Property 'value' of Undefined'. Anyone have any idea ?

  • Matheus, what if you take what was typed in after the "var psw.... that: var password = document.getElementById('password').value; . Is the variable "password" not being recognized?

  • In function function Redirect(err, resp, body, password) you are expecting a return password but in case it is not coming. The ideal is to check the entire return of the request.

  • 1

    What returns in the parameter password function? Place a console.log(password) before the if to verify.

  • @Rodrigotognin tried so too and gave the same mistake

  • @Sam how do I check on the console.log the password if I have to type it into the form ? Sorry, I’m just starting with javascript

  • That one request.get is from where? Some framework?

  • Matheus, I missed a detail... look at this, please: var password = document.getElementById('password'); I was taking the value, actually it was just to get the object since the if accesses the value

  • @Sam yes. (https://www.npmjs.com/package/request)

  • @Rodrigotognin So, apparently the value error has been fixed, however javascript no longer works, it is not checking the password or redirecting

  • @Rodrigotognin lie, the error persists, I was trying in various ways q ended up confusing the codes

  • @Matheusrondow If you assign a function in the form instead of placing the one that is, like this: <form onsubmit="return VerifPass();"> and then in your JS you create this function by putting all this code inside it: function VerifPass(){ ... <código> ... };. In that case, on her return being false, "onsubmit" would not activate..

  • @Rodrigotognin then, is that I can’t test anything while the value error continues, maybe I’m not getting the password typed, but I don’t know how to check it either

  • So Matheus, the code I put in is exactly to get the entered value. Once you put the "getElementById" that I suggested, you take what was typed in the field. So: var psw = document.getElementById('password'); This makes the "Psw" variable the object of the screen field. To access its value, you can use console.log(psw.value);

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.