...

View Full Version : Very Simple Javascript Question Regarding Arrays



Lancerlove
03-16-2012, 02:30 PM
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:

<!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>

webdev1958
03-16-2012, 02:36 PM
your <inputs> and submit button need to be in a <form>

Lancerlove
03-16-2012, 02:45 PM
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.

DaveyErwin
03-16-2012, 02:55 PM
<!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>

webdev1958
03-16-2012, 02:58 PM
Hey thanks for reply,

I did that but still doesn't work. It's only the list[0] that appears as Correct.
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.

Lancerlove
03-16-2012, 03:15 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum