Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Very Simple Javascript Question Regarding Arrays

    Hello guys,
    I'm new to this forum and to Javascript. I need to make a very simple website to check whether a ticket number is valid or not. I have created an array with all the codes necessary, but when I try to loop through it, the only thing that works as it should is the first element of the array.

    Basically, I want the code to display Correct!, if there is a match between user input and array, or Wrong if otherwise.

    Here is my code:
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    
    function validate() {
    var list = new Array;
    list[0]="zDv5BXAc"; 
    list[1]="6YXF3Qwj";
    list[2]="k8UKpgwo";
    var code= document.getElementById("code");
    var name = document.getElementById("name");
    
    for(var i=list.length-1; i>=0; --i) {
    	if (code.value == list[i])
    	{
    		document.all.confirm.innerHTML = "Correct"
    	}
    	else
    		document.all.confirm.innerHTML = "Wrong"
    	}
    }
    </script>
    
    </head>
    
    <body>
    <input name="Name" type="text" id="name" size="50">
    <input name="Code" type="text" id="code" size="10">
    <input name="Check Code" type="submit" value="Verify" onClick="validate()">
    <div id="confirm"></div>
    </body>
    </html>

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    your <inputs> and submit button need to be in a <form>

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by webdev1958 View Post
    your <inputs> and submit button need to be in a <form>
    Hey thanks for reply,

    I did that but still doesn't work. It's only the list[0] that appears as Correct.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    
    function validate() {
    	var list = new Array;
    	list[0]="zDv5BXAc"; 
    	list[1]="6YXF3Qwj";
    	list[2]="k8UKpgwo";
    	var code= document.getElementById("code");
    	var name = document.getElementById("name");
    	var confirm = document.getElementById("confirm");
    
    	for(var i=list.length; i--;) {
    		alert(code.value)
    		if (code.value == list[i])
    		{
    			confirm.innerHTML = "Correct";
    			break;
    		}
    		confirm.innerHTML = "Wrong";	
     	}
    }
    </script>
    
    </head>
    
    <body>
    <input name="Name" type="text" id="name" size="50">
    <input name="Code" type="text" id="code" size="10">
    <input name="Check Code" type="submit" value="Verify" onclick="validate()">
    <div id="confirm"></div>
    </body>
    </html>

  • Users who have thanked DaveyErwin for this post:

    Lancerlove (03-16-2012)

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Lancerlove View Post
    Hey thanks for reply,

    I did that but still doesn't work. It's only the list[0] that appears as Correct.
    Code:
    for(var i=list.length-1; i>=0; --i) {     
    if (code.value == list[i])     {         
    document.all.confirm.innerHTML = "Correct"     }     
    else         document.all.confirm.innerHTML = "Wrong"    
     } 
    }
    Apart from using document.all (which is obsolete - you should be using getElementbyId()) you have a logic error in the FOR loop. On every iteration for a given input, it will output either Correct or Wrong. If the user input matches a value in the array, jump out of the loop and show Correct otherwise show Wrong if no match is found.
    Last edited by webdev1958; 03-16-2012 at 02:00 PM.

  • Users who have thanked webdev1958 for this post:

    Lancerlove (03-16-2012)

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much guys! Both of your solutions worked!

    Awesome help!

    As i'm still here, is there a way to link that code to a name?
    For example, when the code is correct, an innerHTML to write: "CodeNr. : Mr. Name".

    THanks again!
    Last edited by Lancerlove; 03-16-2012 at 02:26 PM.


  •  

    Posting Permissions

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