Comparison of Javascript strings

Asked

Viewed 2,778 times

-1

I am beginning to venture into the world of web programming and am having difficulties with the following subject: I’m creating a password exchange page. I would like my code to compare the login that the user is trying to change the password, with the login that is assigned in my Sesssion, converting the login to lowercase and then making the comparison.

<script>
    function validar(){
        var login = formlogin.nlogin.value;
        var senha = formlogin.nsenha.value;
        var novasenha = formlogin.novasenha.value;
        var logado = "<?php echo $logado?>"

        if (login != logado.toLowerCase()){
            alert('Você não pode alterar a senha para este usuário.');
            formlogin.nlogin.focus;
            return false;
        }
        if (login == ''){
            alert('Preencha o campo de login');
            formlogin.nlogin.focus;
            return false;
        }
        if (senha == ''){
            alert('Preencha o campo de senha');
            formlogin.nsenha.focus;
            return false;
        }
         if (novasenha == ''){
            alert('Preencha o campo Nova senha');
            formlogin.novasenha.focus;
            return false;
        }
    }
</script>

When I do the comparison, even with the string converted to lower case, I get the message that it is impossible to change the user.

Any hint?

  • Where did you come from formlogin?

  • Where did this php syntax come from in the middle of js? var logado = "<?php echo $logado?>"

  • formlogin is the form name. $logged in is the php variable that contains the PHP $_Session username. It was the way I found to pass the value of the $_Session variable to javascript.

  • 4

    you should do it on server code, in your case in php, imagine the password being displayed on html and easy for anyone to see displaying page source code?

  • i put in javascript to put a pop-up warning... I will try to rewrite the code using only php.

  • 1

    It is very dangerous for you to do this check in Javascript. Nowadays, anyone with basic knowledge can delete their checks through the "Inspect Element" function of the browser. As it comes to session, I advise you to do in your PHP script.

  • In your check the var logged in is being passed to lowercase while the var login remains without the same conversion, try to put it in your if login.toLowerCase()

Show 2 more comments

1 answer

-1

Try to take the value of inputs this way:

var login = document.getElementById('nlogin').value;

Browser other questions tagged

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