Everything would be fine, but the problem is with the last else if . It issues an alert with each object, regardless of true or false . And I need to give out once when false .

 $(document).ready(function() { var users = [{ "name": "A", "password": "1" }, { "name": "B", "password": "2" }, { "name": "C", "password": "3" }, { "name": "D", "password": "4" }]; $(document).on("click", ".check-data", function() { var CheckName = $('.check-name').val(); var CheckPassword = $('.check-pass').val(); for (var i = 0; i < users.length; i++) { var DataName = users[i].name; var DataPassword = users[i].password; if (CheckName === DataName && CheckPassword === DataPassword) { alert('Connected'); break; } else if (CheckName === "" && CheckPassword === "") { alert('Required to enter a name and password'); break; } else if (CheckName === DataName && CheckPassword === "") { alert('Required to enter a password'); break; } else if (CheckName === DataName && CheckPassword !== DataPassword) { alert('Wrong password'); break; } else if (CheckName !== DataName) { alert('Name not found'); } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <a>Check Name</a> <input class="check-name"></input> <br> <a>Check Password</a> <input class="check-pass" type="password"></input> <br> <button class="check-data">Check Data in object</button> </form> 

    2 answers 2

    You understand that this method of authorization is useless on the site? Anyone can open the source code of the page and find out the password! Password checking should be done by the server!

    But let's imagine that this example is just for learning JS.

    Do you cycle through the values ​​and make checks that should be at different stages?

    First, it is not necessary to check if the field is empty, with each user, check the values ​​before the cycle:

      if (CheckName === "" ) { alert('Required to enter a name'); return;; } if (CheckPassword === "") { alert('Required to enter a password'); return;; } 

    Secondly, going through the names, it is not necessary to display an error every time. Create a variable that will record whether the user is found:

      var namefound = false; for (var i = 0; i < users.length; i++) { if (CheckName === DataName) { namefound = true; } ... 

    And if there is no match, output an error at the end:

      if (!namefound) { alert('Name not found'); return; } 

     $(document).ready(function() { var users = [{ "name": "A", "password": "1" }, { "name": "B", "password": "2" }, { "name": "C", "password": "3" }, { "name": "D", "password": "4" }]; $(document).on("click", ".check-data", function() { var namefound = false; var CheckName = $('.check-name').val(); var CheckPassword = $('.check-pass').val(); if (CheckName === "") { alert('Required to enter a name'); return; } else if (CheckPassword === "") { alert('Required to enter a password'); return; } for (var i = 0; i < users.length; i++) { var DataName = users[i].name; var DataPassword = users[i].password; if (CheckName === DataName) { namefound = true; } if (CheckName === DataName && CheckPassword === DataPassword) { alert('Connected'); Connected = true; break; } else if (CheckName === DataName && CheckPassword !== DataPassword) { alert('Wrong password'); return; } } if (!namefound) { alert('Name not found'); return; } }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <a>Check Name</a> <input class="check-name"></input> <br> <a>Check Password</a> <input class="check-pass" type="password"></input> <br> <button class="check-data">Check Data in object</button> </form> 

    • exit ? function to be run at the end? - DimenSi
    • @DimenSi I don’t know what the author of the question is planning to do after the entire check, so just in case exit - Crantisz
    • Well, put exit (), otherwise I for example could not understand whether such an operator exists or it is a function :) And I have 2-3 months of experience in js - DimenSi
    • Indeed, confused with php. It's strange that everything works. Replaced with return; - Crantisz
    • Many thanks for the tips! - Nerijus

    You forgot to break write, because of what the cycle is not interrupted.

    IMHO, call the variables differently. CheckName is misleading, it seems that this is a function that checks the name. You can for example testedName or just a name.