user7996412
user7996412

Reputation:

I need to create a new user and password in my object

I need to create a new username and password in my object, when I type it in my text input,after that i need to check it, if they are equal to my username and password in my array object, so the message be YOURE IN, if not equal you need to display message THE PASSWORD OR THE USERNAME IS THE SAME,PLEASE TAKE ANOTHER USERNAME.

When you create a new password & username, I need to added it to the array,after that I need to type it,and if its equal,so i logged in,but I can't make it good,I almost thinking how it works,why when I clicked the button,the function is not works,but in the console I don't see a errors.

window.onload = function(){
      document.getElementById("myButton").addEventListener("click", sys.addUser());
      var newUser = myUser.value;
     var newPass = myPassword.value;
   };

   var sys = {
       users:[
             {username: "alexandr", password:"1334"},
             {username: "evgeny", password:"1345"},
       ],
       addUser: function(username, password){
            var same = false;
            for(var x = 0; x < sys.users.username && sys.users.password; x++){
              if( newUser.length == this.users.username.length[x]){
                  same = true;
                if(same == true){
                    myMessage.innerHTML ="Its a same"; 
                }
                else if(newPass.length == this.users.password.length[x]){
                    same = true;
                    myMessage.innerHTML ="Its a same"; 
                }                   
                else{
                  this.users.username.push(newUser);
                  myMessage.innerHTML = "Ok";

                }
              }
            }

       }

   };

Upvotes: 0

Views: 1342

Answers (3)

tony
tony

Reputation: 2392

There's a lot of errors there, working with your code as it is I turned it into this. Use a code diff tool to see all the changes

As you say, this is a learning exercise so I figured that correcting your code would be of more use to you than showing you a better way to do this.

In the future you'd be well advised to use jsfiddle to come up with a semi working example for people to play with

<html>
<body>
<button type=button id=myButton>click me</button>
<input type="text" id='myUser'/>
<input type="text" id='myPassword'/>
    <span type="text" id='myMessage'></span>

<script>


    var sys = {
        users: [
              { username: "alexandr", password: "1334" },
              { username: "evgeny", password: "1345" },
        ],
        addUser: function (username, password)
        {
            newUser = document.getElementById("myUser").value;
            newPass = document.getElementById("myPassword").value
            myMessage = document.getElementById("myMessage")

            var same = false;
            for (var x = 0; x < sys.users.length; x++)
            {
                if (newUser.length == sys.users[x].username.length)
                {
                    same = true;
                    if (same == true)
                    {
                        myMessage.innerHTML = "Its a same";
                    }
                    else if (newPass.length == sys.users[x].password.length)
                    {
                        same = true;
                        myMessage.innerHTML = "Its a same";
                    }
                }
            }
            if (!same)
            {
                sys.users.push({ username: newUser, password: newPass });
                alert(sys.users.length);
                myMessage.innerHTML = "Ok";
            }               
        }
    };



   window.onload = function(){

    document.getElementById("myButton").addEventListener("click", sys.addUser);
   };

</script>
</body>
</html>

Upvotes: 0

Nenad Vracar
Nenad Vracar

Reputation: 122077

You can use some() method to check if some object has the same password or input as your input values.

var sys = {
  users: [{
    username: "alexandr",
    password: "1334"
  }, {
    username: "evgeny",
    password: "1345"
  }],
  addUser: function() {
    var myUser = document.getElementById('myUser').value
    var myPassword = document.getElementById('myPassword').value
    
    // This will check if object with same username and password exists in array
    var check = this.users.some(function(e) {
    	return e.username == myUser && e.password == myPassword
    })
    
    console.log(check ? 'Correct' : 'Wrong username or password.')
  }
};

// You need to use bind here so that context of this in your method is that object and not element on which you are calling event listener
document.getElementById("myButton").addEventListener("click", sys.addUser.bind(sys));
<input type="text" id="myUser">
<input type="text" id="myPassword">
<button id="myButton">Check</button>

Upvotes: 1

Sunil Singhal
Sunil Singhal

Reputation: 603

Going by your code logically, you are checking the length while you must check on contents. For example, sunils will match with evgeny even though they are different.

Have this: newuser == users.username[x] instead

Upvotes: 0

Related Questions