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?– Rodrigo Tognin
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.– Victor Carnaval
What returns in the parameter
password
function? Place aconsole.log(password)
before theif
to verify.– Sam
@Rodrigotognin tried so too and gave the same mistake
– Matheus Rondow
@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
– Matheus Rondow
That one
request.get
is from where? Some framework?– Sam
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 theif
accesses the value– Rodrigo Tognin
@Sam yes. (https://www.npmjs.com/package/request)
– Matheus Rondow
@Rodrigotognin So, apparently the value error has been fixed, however javascript no longer works, it is not checking the password or redirecting
– Matheus Rondow
@Rodrigotognin lie, the error persists, I was trying in various ways q ended up confusing the codes
– Matheus Rondow
@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 beingfalse
, "onsubmit" would not activate..– Rodrigo Tognin
@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
– Matheus Rondow
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 useconsole.log(psw.value);
– Rodrigo Tognin