www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript look help

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    5

    javascript look help

    Hey guys, i'm fairly new to programming in Javascript.
    I've been trying to write a basic user name and password program.
    I was wondering if I could get some help with how I would put it in a loop so if the information in incorrect, it will prompt up "x" amount of times until the information is correct. Thanks, here's what I have so far.

    var user_name = prompt ("Please enter your username: ");
    var password = prompt ("Please enter your password: ");

    if( (user_name=="John123") && (password=="helloworld") ){
    alert("Welcome, " + user_name + ".");
    }
    else{
    alert (" Sorry, information invalid. " );
    }

  2. #2
    Join Date
    Jul 2011
    Posts
    131
    I think the best way to do it will be recursive function.
    Something like this:
    Code:
    function checkCredentials(){
        var user_name = prompt("Username"),
            password = prompt("password"),
            status = (user_name === "John123") && (password === "helloworld");
        if (!status) {
            checkCredentials();
        } else {
            alert("Successful");
        }
    }
    checkCredentials();

  3. #3
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    A while loop would be much better than a recursive function:
    Code:
    while (true) {
    
      var user_name = prompt("Please enter your username: ");
      var password = prompt("Please enter your password: ");
    
      if (user_name == "John123" && password == "helloworld") {
        alert("Welcome, " + user_name + ".");
        break;
      }
      else {
        alert("Sorry, information invalid.");
      }
    
    }
    This is however a very bad way of coding since it will completely lock the page until the information is correct! You should really use a form with inputs for name and password instead of using prompts.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles